C# Wpf值未传递给viewmodel
我有一个带有文本框的用户控件。textbox文本属性的绑定方式如下:C# Wpf值未传递给viewmodel,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个带有文本框的用户控件。textbox文本属性的绑定方式如下: <TextBox Background="{Binding Path=Background, ElementName=_this}"> <TextBox.Text> <Binding ElementName="_this" Path="VisibleValue" UpdateSourceTrigger="P
<TextBox Background="{Binding Path=Background, ElementName=_this}">
<TextBox.Text>
<Binding ElementName="_this"
Path="VisibleValue"
UpdateSourceTrigger="PropertyChanged"
Mode="TwoWay"
ValidatesOnExceptions="True">
</Binding>
</TextBox.Text>
</TextBox>
“SetValue”方法尝试将字符串转换为int、double等。。。并设置依赖项属性“Value”。如果输入字符串为null或空,“Value”属性将设置为null
public const string ValuePropertyName = "Value";
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
ValuePropertyName,
typeof(Object),
typeof(NumericInput),
new UIPropertyMetadata(null, OnValueChangedCallback));
public Object Value
{
get
{
return (Object)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
现在,在另一个视图中,我正在使用此usercontrol并绑定value属性:
<sharedControls:NumericInput Type="{x:Type sys:UInt16}"
Value="{Binding VehicleLine, TwoWay, PropertyChanged}"/>
我现在的问题是:如果我在文本框中键入一个随机数,如1、2、3、4,该值会像应该的那样传递给我的车辆号。但如果文本框为空,则值依赖项属性设置为null,但这不会传递给我的vehicleline属性。从未调用此属性的setter
谁能解释一下原因吗
注意:即使我将vehicleline属性类型更改为object,也不会调用setter
提前谢谢你 我不知道{Binding VehicleLine,TwoWay,PropertyChanged}是有效的。我认为Path是唯一可以隐式设置的绑定标记扩展属性。您是否尝试过明确地设置模式和UpdateSource触发器?向UIPropertyMetadata添加PropertyChanged回调会由于某种原因阻止调用我的依赖项属性设置程序。当我将代码重构为不使用代码时,它对我起了作用。
<sharedControls:NumericInput Type="{x:Type sys:UInt16}"
Value="{Binding VehicleLine, TwoWay, PropertyChanged}"/>
public ushort VehicleLine
{
get { return this.vehicleLine; }
set
{
if (this.vehicleLine == value) { return; }
this.vehicleLine = value;
RaisePropertyChanged("VehicleLine");
RaiseVariantInfoChanged();
}
}