我创建了一个自定义用户控件,我在我的主 xaml 控件上使用它:
<Controls:CustomControl Width="200" Height="20"
TotalCount="{Binding TotalRecordCount}" SuccessCount="{Binding ValidationCount}" ErrorCount="{Binding ValidationErrorCount}" Margin="0,5,0,0" HorizontalAlignment="Left">
</Controls:CustomControl>
我想让我的自定义用户控件的私有(private)变量为 ErrorCount、SuccessCount 和总计数(它们的类型为 int32)可绑定(bind),以便我可以将值绑定(bind)到它们。现在,当我尝试将其绑定(bind)到我的项目源时,它会给出以下错误 e 异常消息是“'System.Windows.Data.Binding' 类型的对象无法转换为'System.Int32' 类型
非常感谢,
米歇尔
最佳答案
您需要使用 DependencyProperty
实现属性不要使用私有(private)变量来保存这些值。这是一个例子:-
#region public int SuccessCount
public int SuccessCount
{
get { return (int)GetValue(SuccessCountProperty); }
set { SetValue(SuccessCountProperty, value); }
}
public static readonly DependencyProperty SuccessCountProperty =
DependencyProperty.Register(
"SuccessCount",
typeof(int),
typeof(CustomControl),
new PropertyMetadata(0, OnSuccessCountPropertyChanged));
private static void OnSuccessCountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomControl source = d as CustomControl;
int value = (int)e.NewValue;
// Do stuff when new value is assigned.
}
#endregion public int SuccessCount
关于silverlight - 使我的用户控件的属性可绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265084/