WPF 强制重新绑定(bind)

标签 wpf binding

我有一个不能继承 DependencyObject 或使用 NotifyPropertyChanged 的​​对象,并且我已经将它绑定(bind)到很多控件,所以当属性更改时,我不想转到每个控件并更改它在代码上的值,所以我认为必须有一种方法可以告诉 XAML 用一两行代码“重新绑定(bind)”它所绑定(bind)的所有内容,而不是:

label1.Content = myObject.DontNotifyThis;
label2.Content = myObject.DontNotifyThisEither;
label3.Content = myObject.DontEvenThinkOfNotifyingThis;
label4.Content = myObject.NotSoFastPal;

以此类推,以此类推……

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

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>

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;
        }
    }
}

我找到了一种方法,但它一点也不优雅,而且不幸的是,我不能将 DataContext 设置为一个新的天气实例,它需要是相同的引用(这就是为什么我将它设置为 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;
}   

提前致谢!

最佳答案

如果您有权访问要更新绑定(bind)的元素,则可以显式更新绑定(bind)。您可以检索元素上的绑定(bind)表达式,然后使用 UpdateTarget() 刷新 UI,或使用 UpdateSource 刷新支持属性(如果您想绑定(bind)到诸如 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();
    }
}

(如果可能的话,我建议使用 INotifyPropertyChanged。这样你就可以从后面的代码中提取逻辑。)

关于WPF 强制重新绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/957522/

相关文章:

c# - DataGrid不使用ObservableCollection更新

C# 如何提交一个文本框?

java - 将 Swing/FX 与绑定(bind)混合 - 使用自定义属性在线程之间进行调解?

c# - Xaml ListView 异常行为 (Windows Phone 8.1)

c# - MVVM 绑定(bind)到数组索引

c# - WPF UI 仅在调试时崩溃

wpf - WPF按钮内的png图像

c# - 如何在 WPF 中播放 Youtube 视频

wpf - 如何将 Tooltip 的 DataTemplate 与其父级绑定(bind)?

xamarin.forms - 如何为控件模板提供 BindingContext