Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Force rebind
    primarykey
    data
    text
    <p>I have an object that can't inherit DependencyObject OR use NotifyPropertyChanged, and I've binded it to quite a few controls, so when the properties change, I don't want to go to each control and change it's value on the code, so I'm thinking there must be a way to tell the XAML to "Rebind" all that it's bound to with one or two lines of code, instead of going:</p> <pre><code>label1.Content = myObject.DontNotifyThis; label2.Content = myObject.DontNotifyThisEither; label3.Content = myObject.DontEvenThinkOfNotifyingThis; label4.Content = myObject.NotSoFastPal; </code></pre> <p>So on, so forth...</p> <p>This is an oversimplified example:</p> <p>XAML:</p> <pre><code>&lt;Window x:Class="StackOverflowTests.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" x:Name="window1" Height="300" Width="300" Loaded="window1_Loaded"&gt; &lt;Grid x:Name="gridMain"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Label Grid.Row="0" Content="{Binding Status}" ContentStringFormat="Today's weather: {0}" /&gt; &lt;Label Grid.Row="2" Content="{Binding Temperature}" ContentStringFormat="Today's temperature: {0}" /&gt; &lt;Label Grid.Row="1" Content="{Binding Humidity}" ContentStringFormat="Today's humidity: {0}" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>C#:</p> <pre><code>using System.Windows; namespace StackOverflowTests { /// &lt;summary&gt; /// Interaction logic for Window1.xaml /// &lt;/summary&gt; public partial class Window1 : Window { Weather weather = new Weather("Cloudy", "60F", "25%"); public Window1() { InitializeComponent(); this.DataContext = weather; } private void window1_Loaded(object sender, RoutedEventArgs e) { weather.Status = "Sunny"; weather.Temperature = "80F"; weather.Humidity = "3%"; } } class Weather { public string Status { get; set; } public string Temperature { get; set; } public string Humidity { get; set; } public Weather(string status, string temperature, string humidity) { this.Status = status; this.Temperature = temperature; this.Humidity = humidity; } } } </code></pre> <p>I found a way to do it, but it's not elegant at all, and unfortunatelly, I can't just set the DataContext to a new instance of weather, it needs to be the SAME reference (that's why I set it to null so it changes):</p> <pre><code>private void window1_Loaded(object sender, RoutedEventArgs e) { weather.Status = "Sunny"; weather.Temperature = "80F"; weather.Humidity = "3%"; // bad way to do it Weather w = (Weather)this.DataContext; this.DataContext = null; this.DataContext = w; } </code></pre> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload