I typically avoid binding. I'm a bit old school and I've been burned many times by binding but I know I'd benefit from having another tool in my collection. So, I thought I'd try again. Thanks in advance for your help.
I'm comfortable with basic binding. If I have a TextBox and I set the TextBox.Text value like this it seems to work fine:
However, I created a UserControl in a WindowsHelper class called a LabelTextBox. Pretty much any time you show a combobox or textbox (etc) on a form you place a Label to the left of it to explain what it is. So, why not just have one control to define on a form instead of 2?
Now, I want to use DataBinding to show data in the TextBox from my DataContext. This is where it gets a little complicated. I expose a public DependencyProperty named Text from my LabelTextBox. I did this so the programmer can define the UserControl by typing txtName.Text = "Joel". This Text property then needs to bind to the TextBox.Text property inside the LabelTextBox. So, any time a value changes on the ModelView (MV), it needs to propigate from MV --> LabelTextBox.Text --> TextBox.Text and back again.
Anyone have a quick example of how I accomplish this?
I'm comfortable with basic binding. If I have a TextBox and I set the TextBox.Text value like this it seems to work fine:
<TextBox x:Name="txtFirstName" |
Text="{Binding Path=FirstName}" /> |
However, I created a UserControl in a WindowsHelper class called a LabelTextBox. Pretty much any time you show a combobox or textbox (etc) on a form you place a Label to the left of it to explain what it is. So, why not just have one control to define on a form instead of 2?
Now, I want to use DataBinding to show data in the TextBox from my DataContext. This is where it gets a little complicated. I expose a public DependencyProperty named Text from my LabelTextBox. I did this so the programmer can define the UserControl by typing txtName.Text = "Joel". This Text property then needs to bind to the TextBox.Text property inside the LabelTextBox. So, any time a value changes on the ModelView (MV), it needs to propigate from MV --> LabelTextBox.Text --> TextBox.Text and back again.
public string Text |
{ |
get { return (string)GetValue(TextProperty); } |
set { SetValue(TextProperty, value); } |
} |
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... |
public static readonly DependencyProperty TextProperty = |
DependencyProperty.Register("Text", typeof(string), typeof(LabelTextBox)); |
Anyone have a quick example of how I accomplish this?