简体   繁体   English

C#数据绑定不会更新WPF

[英]C# data binding doesn't update WPF

I'm trying to do a Data Binding in the C# code behind rather than the XAML. 我正在尝试在C#代码后面而不是XAML中进行数据绑定。 The XAML binding created in Expression Blend 2 to my CLR object works fine. 在Expression Blend 2中创建的XAML绑定到我的CLR对象可以正常工作。 My C# implementation only updates when the application is started after which subsequent changes to the CLR doesn't update my label content. 我的C#实现仅在应用程序启动时更新,之后对CLR的后续更改不会更新我的标签内容。

Here's the working XAML binding. 这是有效的XAML绑定。 First a ObjectDataProvider is made in my Window.Resources. 首先在我的Window.Resources中创建一个ObjectDataProvider。

<ObjectDataProvider x:Key="PhoneServiceDS" 
    ObjectType="{x:Type kudu:PhoneService}" d:IsDataSource="True"/>

And the label content binding: 和标签内容绑定:

<Label x:Name="DisplayName" Content="{Binding 
    Path=MyAccountService.Accounts[0].DisplayName, Mode=OneWay, 
    Source={StaticResource PhoneServiceDS}}"/>

Works great. 效果很好。 But we want this set up in C# so we can independently change the XAML (ie. new skins). 但我们希望在C#中设置这个,这样我们就可以独立更改XAML(即新皮肤)。 My one time working C# as follows: 我一次在C#上工作如下:

     Binding displayNameBinding = new Binding();
     displayNameBinding.Source = 
         PhoneService.MyAccountService.Accounts[0].DisplayName;
     displayNameBinding.Mode = BindingMode.OneWay;
     this.DisplayName.SetBinding(Label.ContentProperty, displayNameBinding);

This is inside my MainWindow after InitializeComponent(); 这是在InitializeComponent()之后的MainWindow中;

Any insight why this only works on startup? 任何见解为什么这只适用于启动?

Your C# version does not match the XAML version. 您的C#版本与XAML版本不匹配。 It should be possible to write a code version of your markup, though I am not familiar with ObjectDataProvider. 尽管我不熟悉ObjectDataProvider,但应该可以编写标记的代码版本。

Try something like this: 尝试这样的事情:

Binding displayNameBinding = new Binding( "MyAccountService.Accounts[0].DisplayName" );
displayNameBinding.Source = new ObjectDataProvider { ObjectType = typeof(PhoneService), IsDataSource = true };
displayNameBinding.Mode = BindingMode.OneWay;
this.DisplayName.SetBinding(Label.ContentProperty, displayNameBinding);

In the priginal code you have confused the source and path. 在priginal代码中,你混淆了源和路径。

     Binding displayNameBinding = new Binding();
     displayNameBinding.Source = PhoneService;
     displayNameBinding.Path = "MyAccountService.Accounts[0].DisplayName";
     displayNameBinding.Mode = BindingMode.OneWay;
     this.DisplayName.SetBinding(Label.ContentProperty, displayNameBinding);

(I assume PhoneService is an object instance, otherwise perhaps PhoneService. MyAccountService.Accounts[0] should be the Source?) (我假设PhoneService是一个对象实例,否则可能是PhoneService.MyAccountService.Accounts [0]应该是Source?)

From memory, you can pass the path as an argument to the constructor. 从内存中,您可以将路径作为参数传递给构造函数。

Write this inside Loaded event instead of Constructor. 将此内容写入Loaded事件而不是Constructor。 Hope you implmented INotifyPropertyChanged triggered on the DisplayName property setter? 希望您在DisplayName属性setter上触发INotifyPropertyChanged?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM