有没有一种方法可以刷新WPF中的所有绑定?

编程入门 行业动态 更新时间:2024-10-25 07:30:52
本文介绍了有没有一种方法可以刷新WPF中的所有绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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?

服务端:

[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(省略了命名空间样板代码):

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>

客户端代码行为:

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.

推荐答案

找到了答案,似乎是在将PropertyChangedEventArgs属性名称设置为""的情况下调用PropertyChanged会刷新所有绑定. 更改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".

更多推荐

有没有一种方法可以刷新WPF中的所有绑定?

本文发布于:2023-07-19 19:51:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   方法   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!