silverlight - 使我的用户控件的属性可绑定(bind)

标签 silverlight

我创建了一个自定义用户控件,我在我的主 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/

相关文章:

c# - 为什么 KeyDown 事件无法访问绑定(bind)变量的当前值?

.net - Silverlight OOB 或 WebHosted 可以在 x64 平台下构建吗?

multithreading - Silverlight Task.WaitAll使用Rx

c# - 未找到数据绑定(bind)属性,为什么?

c# - RIA 身份验证期间出现问题

wpf - 如何在silverlight中在网格周围设置阴影?

Silverlight 5 最终发布日期

wpf - 在 WPF 中的 MVVM 中的 Viewmodel 之间传递值

silverlight - ClientBin 出现 404 错误

wpf - 基于可见性停止/开始 Storyboard动画