似乎在WPF中,我不能绑定到对象上的公共字段,而只能绑定到公共属性。这是WPF故意设计的决定,还是我只是把语法搞错了?
下面是一个示例代码片段:
public class User
{
public string Username;
public string FullName;
public string DisplayName
{
get { return FullName; }
}
}
WPF代码段:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&
启动新的Silverlight应用程序...在后面的代码中(在“已加载”事件中),放入下面的代码:
// This will *NOT* cause an error.
this.LayoutRoot.DataContext = new string[5];
但是..。
// This *WILL* cause an error!
this.LayoutRoot.DataContext = this;
出现的错误是“值不在预期范围内”。顺便说一句,此代码在常规WPF (Windows开发)中100%工作,没有理由不能在WPF/E中工作:)
有什么想法吗?
我在WPF中有两个依赖属性的实现。首先,我在网上发现:
public class TestClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
priva
在这种情况下,有一点不知道如何使用WPF绑定:
假设有一个具有CarInfo类型的非简单属性的对象Car:
public class CarInfo : DependencyObject
{
public static readonly DependencyProperty MaxSpeedProperty =
DependencyProperty.Register("MaxSpeed", typeof (double), typeof (CarInfo), new PropertyMetadata(0.0));
public double M