绑定到 Nullable Int32 时的绑定表达式错误
Binding Expression Error When Binding to Nullable Int32
所以我认为我在 UWP 平台中发现了一个非常有趣的错误。
如果我有一个 textbox
并且我将它的 Text
属性 绑定到一个 int?
依赖项 属性,那么我会得到以下异常。如果我的消费者将可空 int 或不可空 int 绑定到控件似乎并不重要,都会显示相同的错误。看起来它与可空的依赖项 属性 直接相关。
Error: Converter failed to convert value of type 'Windows.Foundation.Int32'
to type 'IReference`1<Int32>'; BindingExpression: Path='MyNotNullableInt'
DataItem='ControlSandbox.FooViewModel'; target element is
'ControlSandbox.NumericTextBox' (Name='null');
target property is 'Value' (type 'IReference`1<Int32>').
我什至没有使用转换器,所以我猜这是框架内部发生的事情。下面的示例代码将产生输出
Main.xaml
<Grid Background="White" >
<local:NumericTextBox Value="{Binding MyNotNullableInt, Mode=TwoWay}" />
</Grid>
Main.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.DataContext = new FooViewModel();
}
}
FooViewModel.cs
public class FooViewModel : INotifyPropertyChanged
{
private int _myNotNullableInt;
public int MyNotNullableInt
{
get { return _myNotNullableInt; }
set { _myNotNullableInt = value; OnPropertyChanged("MyNotNullableInt"); }
}
private int? _myNullableInt;
public int? MyNullableInt
{
get { return _myNullableInt; }
set { _myNullableInt = value; OnPropertyChanged("MyNullableInt"); }
}
public FooViewModel()
{
MyNullableInt = null;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
NumericTextBox.xaml
<Grid>
<TextBox Text="{x:Bind Value}" />
</Grid>
NumericTextBox.xaml.cs
public sealed partial class NumericTextBox : UserControl
{
public int? Value
{
get { return (int?)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int?), typeof(NumericTextBox), new PropertyMetadata(0));
public NumericTextBox()
{
this.InitializeComponent();
}
}
所以我不确定为什么在我们对错误进行研究的最初几个小时没有返回此页面,但是,至少 post 现在将记录下来以备将来使用。
我正在研究绑定到可为 null 的依赖属性,我遇到了 this article
Turns out that under the covers most of the .NET primitive types are
converted to equivalent Windows Runtime types. IReference happens to
be the Windows Runtime equivalent of Nullable in .NET.
它说,您需要将依赖性 属性 上的 属性 类型 typeof
从可空类型更改为 object
我已经确认这有效。
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(NumericTextBox), new PropertyMetadata(0));
所以我认为我在 UWP 平台中发现了一个非常有趣的错误。
如果我有一个 textbox
并且我将它的 Text
属性 绑定到一个 int?
依赖项 属性,那么我会得到以下异常。如果我的消费者将可空 int 或不可空 int 绑定到控件似乎并不重要,都会显示相同的错误。看起来它与可空的依赖项 属性 直接相关。
Error: Converter failed to convert value of type 'Windows.Foundation.Int32'
to type 'IReference`1<Int32>'; BindingExpression: Path='MyNotNullableInt'
DataItem='ControlSandbox.FooViewModel'; target element is
'ControlSandbox.NumericTextBox' (Name='null');
target property is 'Value' (type 'IReference`1<Int32>').
我什至没有使用转换器,所以我猜这是框架内部发生的事情。下面的示例代码将产生输出
Main.xaml
<Grid Background="White" >
<local:NumericTextBox Value="{Binding MyNotNullableInt, Mode=TwoWay}" />
</Grid>
Main.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.DataContext = new FooViewModel();
}
}
FooViewModel.cs
public class FooViewModel : INotifyPropertyChanged
{
private int _myNotNullableInt;
public int MyNotNullableInt
{
get { return _myNotNullableInt; }
set { _myNotNullableInt = value; OnPropertyChanged("MyNotNullableInt"); }
}
private int? _myNullableInt;
public int? MyNullableInt
{
get { return _myNullableInt; }
set { _myNullableInt = value; OnPropertyChanged("MyNullableInt"); }
}
public FooViewModel()
{
MyNullableInt = null;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string prop)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
NumericTextBox.xaml
<Grid>
<TextBox Text="{x:Bind Value}" />
</Grid>
NumericTextBox.xaml.cs
public sealed partial class NumericTextBox : UserControl
{
public int? Value
{
get { return (int?)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int?), typeof(NumericTextBox), new PropertyMetadata(0));
public NumericTextBox()
{
this.InitializeComponent();
}
}
所以我不确定为什么在我们对错误进行研究的最初几个小时没有返回此页面,但是,至少 post 现在将记录下来以备将来使用。
我正在研究绑定到可为 null 的依赖属性,我遇到了 this article
Turns out that under the covers most of the .NET primitive types are converted to equivalent Windows Runtime types. IReference happens to be the Windows Runtime equivalent of Nullable in .NET.
它说,您需要将依赖性 属性 上的 属性 类型 typeof
从可空类型更改为 object
我已经确认这有效。
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(NumericTextBox), new PropertyMetadata(0));