Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Property of my Usercontrol Bindable

Tags:

silverlight

i have created a custom user control which im using on my main xaml control:

<Controls:CustomControl  Width="200" Height="20" 
TotalCount="{Binding TotalRecordCount}" SuccessCount="{Binding ValidationCount}" ErrorCount="{Binding ValidationErrorCount}" Margin="0,5,0,0"  HorizontalAlignment="Left"> 
</Controls:CustomControl>

I wanted to make the private variables of my custom usercontrol being ErrorCount,SuccessCount and total count(which are of type int32) Bindable so i can bind values to them. Right now when i try to bind it to my item source it gives the following error e exception message is "Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Int32'

Many thanks, Michelle

like image 772
Michelle Avatar asked Jun 07 '11 12:06

Michelle


1 Answers

You need to implement the Properties using DependencyProperty don't use private variables to hold these values. Here is an example:-

    #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
like image 67
AnthonyWJones Avatar answered Nov 15 '22 06:11

AnthonyWJones