在具有不同数据类型的基类和派生类中,我都有一个同名的属性。将属性绑定到TextBlock.Text属性时,绑定自身会发生异常。
注意:仅在UWP中引发异常。但是在WPF中效果很好。
例外屏幕截图:
ambiguousmatchexception.png
BindingCode:
MyData myDataObject = new MyData(DateTime.Now);
Binding myBinding = new Binding();
myBinding.Mode = BindingMode.TwoWay;
myBinding.ConverterParameter = null;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.Default;
myBinding.Path = new PropertyPath("MyDataProperty");
myBinding.Source = myDataObject;
myBinding.ElementName = myDataObject.MyDataProperty;
textBlock.SetBinding(TextBlock.TextProperty, myBinding);
型号:
public class data
{
public int MyDataProperty { get; set; }
}
public class MyData : data
{
private string myDataProperty;
enter code here
public MyData() { }
public MyData(DateTime dateTime)
{
myDataProperty = "Last bound time was " + dateTime.ToLongTimeString();
}
public new String MyDataProperty
{
get { return myDataProperty; }
set
{
myDataProperty = value;
OnPropertyChanged("MyDataProperty");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string info)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(info));
}
}