This is a migrated thread and some comments may be shown as answers.

Multi-Level Data Binding

5 Answers 86 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joel Palmer
Top achievements
Rank 2
Joel Palmer asked on 17 Jan 2010, 10:18 PM
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:
<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?

5 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 19 Jan 2010, 11:27 AM
Hello Joel Palmer,

When registering the Text  dependency property of your user control , please add a DependencyPropertyChanged callback .

Within that callback you can update the value of the inner TextBox without any intenral binding needed.

Let me know if you need a sample on that.

Kind regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Joel Palmer
Top achievements
Rank 2
answered on 21 Jul 2010, 05:12 PM
I've tried this 3 different ways now.  Can you provide a working example?  What I have:

private static void OnTextPropertyChanged(
    DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    // Uncomment this line to represent which control is being tied in.
    // Update the associated control's property when the Model View has updated this property.
    ((LabelTextBox)sender).txt.Text = args.NewValue.ToString();
    if (args.OldValue != null)
    {
        OriginalValue = args.OldValue.ToString();
    }
    // Update the property on the object that is bound to it when the View Model has updated this property.
    object dataContext = ((System.Windows.FrameworkElement)(sender)).DataContext;
    if (dataContext != null)
    {
        BindingExpression bindingExpression = ((BindingExpression)((LabelTextBox)sender).GetBindingExpression(TextProperty));   
        string propertyName = bindingExpression.ParentBinding.Path.Path;
        PropertyInfo prop = bindingExpression.DataItem.GetType().GetProperty(propertyName);
        prop.SetValue(bindingExpression.DataItem, args.NewValue, new object[]{});
    }
}
0
Joel Palmer
Top achievements
Rank 2
answered on 26 Jul 2010, 03:47 AM
Do you have a working example of this?  I have success updating the UserControl.Control.Text (MV) from the source.  However, even TwoWay binding doesn't seem to update the String.Property when I change the Control.Text.

<UserControl>
<Control.Text />
<DependencyProperty TextProperty />
</UserControl>
<Class>
<String Property />
</Class>

Once again, I want to map my "String Property" to the "Control.Text" property.  The UserControl contains a TextProperty that is exposed to the public.  When the Class String Property is updated I want the value to be shown in the Control.Text and when the Control.Text changes, I want it to propogate to the Class String Property.

So in the XAML, I bind the UserControl.TextProperty to the Class String Property.  Text={Binding Path=Text, Mode=TwoWay}
0
Miroslav
Telerik team
answered on 28 Jul 2010, 08:47 AM
Hello Joel,

I am sorry for the delayed reply!

It seems that what you need here is a Control and not UserControls.

The problem here is that you cannot easily bind a property in the UserControl's content to one of its dependency properties.

It is possible if it has an internal ViewModel but that is a complication that  IMO is not offset in any way from the fact that you are using a UserControl.

I created a simple control that is matches what you describe - I hope that it will be useful.

Kind regards,
Miroslav
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Joel Palmer
Top achievements
Rank 2
answered on 28 Jul 2010, 10:30 PM
Thats an unexpected twist.  I guess I never considered "Control" instead of "UserControl".  I'll take a look.  Thanks.
Tags
General Discussions
Asked by
Joel Palmer
Top achievements
Rank 2
Answers by
Pavel Pavlov
Telerik team
Joel Palmer
Top achievements
Rank 2
Miroslav
Telerik team
Share this question
or