WPF Force重新绑定

Car*_*rlo 14 wpf binding

我有一个不能继承DependencyObject的对象或者使用NotifyPropertyChanged,我把它绑定到了很多控件,所以当属性改变时,我不想去每个控件并改变它在代码上的值,所以我认为必须有一种方法可以告诉XAML"重新绑定"所有与一两行代码绑定的内容,而不是去:

label1.Content = myObject.DontNotifyThis;
label2.Content = myObject.DontNotifyThisEither;
label3.Content = myObject.DontEvenThinkOfNotifyingThis;
label4.Content = myObject.NotSoFastPal;
Run Code Online (Sandbox Code Playgroud)

等等......等等......

这是一个过于简单的例子:

XAML:

<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">
    <Grid x:Name="gridMain">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="{Binding Status}" ContentStringFormat="Today's weather: {0}" />
        <Label Grid.Row="2" Content="{Binding Temperature}" ContentStringFormat="Today's temperature: {0}" />
        <Label Grid.Row="1" Content="{Binding Humidity}" ContentStringFormat="Today's humidity: {0}" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

C#:

using System.Windows;

namespace StackOverflowTests
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到了一种方法,但它根本不优雅,不幸的是,我不能只将DataContext设置为一个新的天气实例,它需要是SAME引用(这就是为什么我将它设置为null所以它变化):

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;
}   
Run Code Online (Sandbox Code Playgroud)

提前致谢!

rmo*_*ore 20

如果您有权访问要更新绑定的元素,则可以显式更新绑定.您可以在元素上检索Binding Expression,然后使用UpdateTarget()刷新UI,或使用UpdateSource刷新支持属性(如果要绑定到可编辑的内容,如TextBox).

这是一个演示它的简单示例:

<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();
    }
}
Run Code Online (Sandbox Code Playgroud)

(我建议尽可能使用INotifyPropertyChanged.这样你可以从后面的代码中提取逻辑.)