Making Property of my Usercontrol Bindable
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 pri开发者_StackOverflowvate 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
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
In order for a property to be "Bindable", meaning that you can set it using DataBinding, that property needs to be a Dependency Property
. For more info on Dependency Properties, please check this MSDN article.
hope this helps :)
精彩评论