有没有办法刷新 WPF 中的所有绑定?

发布于 2024-10-20 01:42:59 字数 1617 浏览 2 评论 0原文

如果我的代码看起来有点像下面的代码,是否可以直接刷新所有绑定,或者我是否必须对所有绑定进行硬编码才能刷新?

服务端:

[ServiceContract]
public interface IMyServiceContract {
    [OperationContract]
    MyDataContract GetData();
}
[ServiceBehavior]
public class MyService {
    [OperationBehavior]
    public MyDataContract GetData() {
        MyDataContract data = new MyDataContract();
        data.val1 = "123";
        data.val2 = "456";
        return data;
    }
}
[DataContract]
public class MyDataContract {
    [DataMember]
    public string val1;
    public string val2;
}

客户端 xaml(省略命名空间样板代码):

<Window x:Class="MyWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="{Binding Path=val1, Mode=OneWay}">
    <DockPanel>
        <TextBlock Text="{Binding Path=val1, Mode=OneWay}"/>
        <TextBlock Text="{Binding Path=val2, Mode=OneWay}"/>
    </DockPanel>
</Window>

客户端代码行为:

public partial class MyWindow {
    MyServiceClient client = new MyServiceClient();
    MyDataContract data;
    public string val1 {get{return data.val1;}}
    public string val2 {get{return data.val2;}}
    DispatcherTimer updateTimer = new DispatcherTimer();

    public MyWindow() {
        timer.Interval = new TimeSpan(0, 0, 10);
        timer.Tick += new EventHandler(Tick);
        Tick(this, null);
        timer.Start();
        InitializeComponent();
    }

    void Tick(object sender, EventArgs e) {
        data = client.GetData();
        // Refresh bindings
    }
}

请忽略示例代码中的任何编码标准违规,因为它只是作为预期用途的示例。

If my code looks somewhat like the code beneath, would it be possible to refresh all bindings directly or would I have to hard-code all the bindings to refresh?

Service-side:

[ServiceContract]
public interface IMyServiceContract {
    [OperationContract]
    MyDataContract GetData();
}
[ServiceBehavior]
public class MyService {
    [OperationBehavior]
    public MyDataContract GetData() {
        MyDataContract data = new MyDataContract();
        data.val1 = "123";
        data.val2 = "456";
        return data;
    }
}
[DataContract]
public class MyDataContract {
    [DataMember]
    public string val1;
    public string val2;
}

Client-side xaml (namespace boilerplate code omitted):

<Window x:Class="MyWindow" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="{Binding Path=val1, Mode=OneWay}">
    <DockPanel>
        <TextBlock Text="{Binding Path=val1, Mode=OneWay}"/>
        <TextBlock Text="{Binding Path=val2, Mode=OneWay}"/>
    </DockPanel>
</Window>

Client-side code-behing:

public partial class MyWindow {
    MyServiceClient client = new MyServiceClient();
    MyDataContract data;
    public string val1 {get{return data.val1;}}
    public string val2 {get{return data.val2;}}
    DispatcherTimer updateTimer = new DispatcherTimer();

    public MyWindow() {
        timer.Interval = new TimeSpan(0, 0, 10);
        timer.Tick += new EventHandler(Tick);
        Tick(this, null);
        timer.Start();
        InitializeComponent();
    }

    void Tick(object sender, EventArgs e) {
        data = client.GetData();
        // Refresh bindings
    }
}

Please disregard any coding standards violations in the example code since it is simply intended as an example for the intended use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾忌 2024-10-27 01:42:59

找到了答案,似乎调用 PropertyChanged 并将 PropertyChangedEventArgs 属性名称设置为 "" 会刷新所有绑定。
DataContext 的更改也有效,尽管这感觉有点“干净”。

Found the answer, seems like that calling PropertyChanged with the PropertyChangedEventArgs property name set to "" refreshes all bindings.
The DataContext changing worked too, although this felt a bit "cleaner".

逆流 2024-10-27 01:42:59

您可以为 null,然后重新设置父对象的 DataContext。

DataContext = null;
DataContext = data;

You can null then re-set the DataContext of the parent object.

DataContext = null;
DataContext = data;
任性一次 2024-10-27 01:42:59

如何使“数据”成为依赖属性?将您的 DataContext 绑定到它将使您的绑定在您重新分配“数据”时更新。

How about making "data" a dependency property. Binding your DataContext to that will make your bindings update when you re-assign "data".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文