Custom user control binding
-
Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????
-
Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????
I have doubt with line:
public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1),
new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));As per my knowledge UIPropertyMetadata is mainly used for provide animation behavioure. I would suggest try with FrameworkPropertyMetaData for databinding purpose. Just try on your side if still it is not working then I will look into that. http://msdn.microsoft.com/en-us/library/ms751554.aspx[^] Thanks for your patience!
Parwej Ahamad ahamad.parwej@gmail.com
modified on Tuesday, March 30, 2010 9:53 AM
-
I have doubt with line:
public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1),
new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack)));As per my knowledge UIPropertyMetadata is mainly used for provide animation behavioure. I would suggest try with FrameworkPropertyMetaData for databinding purpose. Just try on your side if still it is not working then I will look into that. http://msdn.microsoft.com/en-us/library/ms751554.aspx[^] Thanks for your patience!
Parwej Ahamad ahamad.parwej@gmail.com
modified on Tuesday, March 30, 2010 9:53 AM
Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you
-
Dear friends i have an issue .. i am new to WPF, my requirement is to (binding "custom property" for user control) can some one point out my mistake? User Control ------------- UserControl Code behind -------------------------- public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); int nType =0; public int CoType { get {return nType;} set { nType =value; textBlock1.Text = value.ToString(); } } static void textChangedCallBack(DependencyObject property,DependencyPropertyChangedEventArgs args) { UserControl1 searchTextBox = (UserControl1)property; searchTextBox.CoType = (int)args.NewValue; } } Main Form ----------- Main Form Code behind is just InitializeComponent() ViewModel -------------- public int CoTypeValue { get { return nV; } set { if (value != nV) { nV = value; OnPropertyChanged("CType"); } } } The Error message i recive Cannot create instance of 'UserControl1' defined in assembly 'WpfModelViewApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error at object 'System.Windows.Controls.Grid' in markup file If i remove the Dependency property .. the error is not thrown .. but i want to binding for custom property.. and i not sure about the way i have implemented binding for custom property is the right way.. any help????
I don't know exactly why you got this error message, but your "dependency property" is NOT a properly declared and used dependency property! use the "propdp" snippet in VS to declare one
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you
Please send me your sample code on ahamad.parwej@gmail.com.
Parwej Ahamad ahamad.parwej@gmail.com
-
Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you
Hi Joe, I catch one more thing here you are not using Setter and Getter method:
public int CoType
{
get {return nType;}
set {
nType =value;
textBlock1.Text = value.ToString();
}
}Should be like:
public int CoType
{
get { return (int)GetValue(coProperty ); }
set { SetValue(coProperty , value); }
}Change your code and let me know.
Parwej Ahamad ahamad.parwej@gmail.com
-
Dear friend Thanks for your reply , i have changed those lines as public static readonly DependencyProperty coProperty = DependencyProperty.Register("CoType", typeof(int), typeof(UserControl1), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(textChangedCallBack))); but no use, if you have already done some samples on Custom Property ..please send it to i.joerozario@gmail.com other wise i will send my sample source.. if u correct that would be great help for me.. Thank you
Basic sample sent on your personal ID.
Parwej Ahamad ahamad.parwej@gmail.com
-
Basic sample sent on your personal ID.
Parwej Ahamad ahamad.parwej@gmail.com
Dear Ahamad Thanks for everything.. i found my mistake. in the Usercontrol1.xaml i did not bind the text like Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CoType}" Thank you by Joe