我在Uwp用户控件中有一个ComboBox,我想将SelectedValue属性直接绑定到控件的Dependency属性,所以我可以直接设置/获取值
这是用户控件的内容
<Grid>
<ComboBox x:Name="clientiList" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top"
Width="300"
SelectedValuePath="ClienteNo"
DisplayMemberPath="Descrizione"
SelectedValue="{Binding idCliente, ElementName=userControl, Mode=TwoWay}" />
</Grid>
这是控件中的代码
public int ?idCliente {
get { return IdCliente; }
set { IdCliente = value; }
}
public int ?IdCliente
{
get { return (int?)GetValue(IdClienteProperty); }
set { SetValue(IdClienteProperty, value); }
}
public static readonly DependencyProperty IdClienteProperty =
DependencyProperty.Register("IdCliente", typeof(int?), typeof(ClientiAttiviSelect), new PropertyMetadata(null));
我看到如果我在列表中选择一个值时将SelectedValue属性绑定到 IdCliente 属性,我会在输出窗口中看到此错误
Error: Converter failed to convert value of type 'Windows.Foundation.Int32' to type 'IReference`1<Int32>'; BindingExpression: Path='IdCliente' DataItem='MyAttivita.Features.Cliente.Views.ClientiAttiviSelect'; target element is 'Windows.UI.Xaml.Controls.ComboBox' (Name='clientiList'); target property is 'SelectedValue' (type 'Object').
如果我将SelectedValue绑定到 idCliente 属性并使用它来更新 IdCliente 属性,则它可以正常工作。
为什么会这样?我怎样才能避免这种重复?