Witam mam problem z automatycznym updatem formularza.
Generalnie kod działa tak jak należy tylko nie odświeża się checkbox.
Model.cs:
namespace BindingsTest
{
class Model
{
public bool test1;
public bool test2;
}
}
ModelView.cs
using System;
using System.ComponentModel;
namespace BindingsTest
{
class ModelView
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChanged?.Invoke(this, e); }
Model model = new Model();
// Założenie jest takie że test2 może się wykonac tylko jeżeli będzie wykoanany test1
// Jeżeli ktoś chce zaznaczyć test2 to automatycznie zostanie zaznaczony test1.
// Jeżeli ktoś od-zaznaczy (ustawi false) test1 to automatycznie ustawi się na false test2
public bool Test1 {
get { return model.test1; }
set
{
model.test1 = value;
if (!value) {
model.test2 = value;
}
OnPropertyChanged(new PropertyChangedEventArgs("Test1"));
}
}
public bool Test2
{
get { return model.test2; }
set
{
model.test2 = value;
if (value)
{
model.test1 = value;
}
OnPropertyChanged(new PropertyChangedEventArgs("Test2"));
}
}
}
}
xaml:
<Window x:Class="BindingsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingsTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Margin="15">
<WrapPanel Margin="10,10,10,0">
<CheckBox IsChecked="{Binding Test1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="Test podstawowy"/>
</WrapPanel>
<WrapPanel Margin="10,10,10,0">
<CheckBox IsChecked="{Binding Test2, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="Test dodatkowy"/>
</WrapPanel>
<WrapPanel Margin="10,10,10,0">
<Button Content="Pokaż stan" Click="Button_Click" Width="100"/>
</WrapPanel>
<WrapPanel Margin="10,10,10,0">
<Label Name="a" Content="Label 1" VerticalAlignment="Top" Width="300"/>
<Label Name="b" Content="Label 2" VerticalAlignment="Top" Width="300"/>
</WrapPanel>
</StackPanel>
</Grid>
</Window>
view:
using System.Windows;
namespace BindingsTest
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ModelView mymodel = new ModelView();
public MainWindow()
{
InitializeComponent();
this.DataContext = mymodel;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
a.Content = mymodel.Test1;
b.Content = mymodel.Test2;
}
}
}
Co robię źle ?