I have a very simple piece of code to understand the behavior that what happens when we have assigned a Binding expression to any dependency property and then assign a direct value to that dependency property. Following is code
View XAML
<StackPanel>
<Button Click="Button_Click" Content="Assign binding value" />
<Button Click="Button_Click_1" Content="Assign direct value" />
<TextBox Text="{Binding TextSource, Mode=OneWay}" x:Name="stf" />
</StackPanel>
View XAML.cs
public partial class MainWindow : Window
{
MainViewViewModel vm = new MainViewViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.TextSource = "Value set using binding";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
stf.Text = "New direct value";
}
}
ViewModel
public class MainViewViewModel : INotifyPropertyChanged
{
//INotifypropertychanged implementation here ...
private string _textSource;
public string TextSource
{
get { return _textSource; }
set
{
_textSource = value;
OnPropertyChanged("TextSource");
}
}
}
Now my observation is
- When I click "Assign binding value" the view is updated with binding source value. (As expected)
- When I click "Assign direct value" the view is updated with the direct value assigned to text field (As expected)
- I assume that at this stage the binding is broken and when I click on "Assign binding value" again it should not work, mean no UI update. and it works according to my expectations (As expected)
- The confusing point is when I set the binding mode to "TwoWay", then point 3 is not happening, and it always keeps working whatever button I press. both from binding source and direct value. (Not clear to me what TwoWay binding need to do with this)
Anyone please clarify it?