I have a problem with TwoWay binding porcess.
On the code I have a TextBox, Button and 3 checkBox
I want to update the TextBox when i press the button.
When i use the checkbox i define TwoWay binding for each checkbox and I also define the Click event (for the main checkbox - ValidatAll). In that way a click on a checkbox effect the other checkboxes (for example if all values are false, a click of Validate all will turn all of them to true and the checkboxes will show as checked)
The problem is when i try to do the same trick on the TextBox using the button as triger.
The button open a file browser that return a string. i take this string and issue it to the TextBox.Text field. this Field is define as TwoWay Binding...
The problem is that this code update just the Gui, but it doesn't update the logic (it seems like i am missing an event here).
For example lets say i want to issue c:\temp into the TextBox.
If i will write "c:\temp", the "WindowsRootProperty" will get the string value.
When i use the browsing button, the TextBox.Text is updated but the "WindowsRootProperty" never called...
can anyone help?
see attached code
i am using WPF - .NET3.5
here is my code sample (I reduce the code by removing the non-relevant design code):
Window1.xaml:
Expand|Select|Wrap|Line Numbers
- <Window x:Class="scomp.Window1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:src="clr-namespace:scomp"
- <Window.Resources>
- <src:UserData x:Key="myUserData"/>
- </Window.Resources>
- <Window.DataContext>
- <Binding Source="myUserData"/>
- </Window.DataContext>
- <Grid Name="scomp">
- <TextBox Name="winRootData"
- Text="{Binding Path=WindowsRootProperty, Source={StaticResource myUserData}, Mode=TwoWay}">
- </TextBox>
- <Button Name="winRootButton"
- Height="30"
- Width="90"
- Click="winRootButton_Click">Browse...
- </Button>
- <CheckBox x:Name="VlaidateAll"
- Content="Validation"
- Margin="30,0,0,0"
- IsChecked="{Binding Path=AllValidateProperty, Source={StaticResource myUserData}, Mode=TwoWay}"
- Click="VlaidateAll_Click">
- </CheckBox>
- <CheckBox x:Name="InputValidation"
- Content="Input Validation"
- IsChecked="{Binding Path=InputValidateProperty, Source={StaticResource myUserData}, Mode=TwoWay}">
- </CheckBox>
- <CheckBox x:Name="TriggerValidation"
- Content="Trigger Validation"
- IsChecked="{Binding Path=TriggerValidateProperty, Source={StaticResource myUserData}, Mode=TwoWay}" >
- </CheckBox>
- </Grid>
- </Window>
- Window1.xaml.cs:
- public partial class Window1 : Window
- {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- public Window1()
- {
- InitializeComponent();
- }
- private void VlaidateAll_Click(object sender, RoutedEventArgs e)
- {
- if ((sender as System.Windows.Controls.CheckBox).IsChecked == true)
- {
- this.InputValidation.IsChecked = true;
- this.TriggerValidation.IsChecked = true;
- }
- else
- {
- this.InputValidation.IsChecked = false;
- this.TriggerValidation.IsChecked = false;
- }
- }
- private void winRootButton_Click(object sender, RoutedEventArgs e)
- {
- fbd.SelectedPath = this.winRootData.Text;
- fbd.ShowNewFolderButton = false;
- fbd.ShowDialog();
- this.winRootData.Text = fbd.SelectedPath;
- fbd.SelectedPath = null;
- }
- }
- UserData.cs:
- public class UserData : INotifyPropertyChanged
- {
- private bool? vAllValidate;
- private bool? vInputValidate;
- private bool? vTriggerValidate;
- private string mWinRoot;
- public event PropertyChangedEventHandler PropertyChanged;
- public UserData()
- {
- vAllValidate = false;
- vInputValidate = false;
- vTriggerValidate = false;
- mWinRoot = null;
- }
- public string WindowsRootProperty
- {
- set
- {
- mWinRoot = value;
- OnPropertyChanged("WindowsRootProperty");
- }
- get
- {
- return mWinRoot;
- }
- }
- public bool? AllValidateProperty
- {
- set
- {
- vAllValidate = value;
- OnPropertyChanged("AllValidateProperty");
- }
- get
- {
- return vAllValidate;
- }
- }
- public bool? InputValidateProperty
- {
- set
- {
- vInputValidate = value;
- OnPropertyChanged("InputValidateProperty");
- if (value == true)
- {
- if (vAllValidate == false)
- {
- AllValidateProperty = true;
- }
- }
- else
- {
- if (vAllValidate == true && vTriggerValidate == false)
- {
- AllValidateProperty = false;
- }
- }
- }
- get
- {
- return vInputValidate;
- }
- }
- public bool? TriggerValidateProperty
- {
- set
- {
- vTriggerValidate = value;
- OnPropertyChanged("TriggerValidateProperty");
- if (value == true)
- {
- if (vAllValidate == false)
- {
- AllValidateProperty = true;
- }
- }
- else
- {
- if (vAllValidate == true && vInputValidate == false)
- {
- AllValidateProperty = false;
- }
- }
- }
- get
- {
- return vTriggerValidate;
- }
- }
- protected void OnPropertyChanged(string str)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(str));
- }
- }
- }