有没有办法刷新 WPF 中的所有绑定?
如果我的代码看起来有点像下面的代码,是否可以直接刷新所有绑定,或者我是否必须对所有绑定进行硬编码才能刷新?
服务端:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到了答案,似乎调用 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".
您可以为 null,然后重新设置父对象的 DataContext。
You can null then re-set the DataContext of the parent object.
如何使“数据”成为依赖属性?将您的 DataContext 绑定到它将使您的绑定在您重新分配“数据”时更新。
How about making "data" a dependency property. Binding your DataContext to that will make your bindings update when you re-assign "data".