WPF Force rebind WPF Force rebind wpf wpf

WPF Force rebind


If you have access to the element that you want to update the binding on then you can explicitly update the binding. You can retrieve the Binding Expression on the element and then use UpdateTarget() to refresh the UI, or UpdateSource to refresh the backing property (if you want to bind to something editable like a TextBox).

Here's a simple example that demonstrates it:

<StackPanel>    <TextBlock x:Name="uiTextBlock" Text="{Binding MyString}" />    <Button Click="Button_Click"            Content="Rebind" /></StackPanel>public partial class Window1 : Window{    public string MyString { get; set; }    public Window1()    {        MyString = "New Value";        InitializeComponent();        this.DataContext = this;    }    int count = 0;    private void Button_Click(object sender, RoutedEventArgs e)    {        MyString = "Rebound " + ++count + " times";        var bindingExpression = uiTextBlock.GetBindingExpression(TextBlock.TextProperty);        bindingExpression.UpdateTarget();    }}

(I would recommend using INotifyPropertyChanged though if at all possible. That way you can extract the logic from the code behind.)