I have a data model (not really a view model; it does not directly contain dependency properties, and I want to keep it that way), which structurally looks something like this:
public class Foo
{
public Bar Data { get; set; }
// other properties
}
public class Bar
{
public string Text { get; set; }
// other properties
}
And a custom UserControl
that looks somewhat like this:
<MyUserControl ...>
<Grid>
<TextBox Text="{Binding ???}">
</Grid>
</MyUserControl>
The code-behind of the user control contains a reference to an instance of Foo
.
How would I go about binding the Text
value of the TextBox
inside the MyUserControl
to the Data.Text
property of the Foo
object referenced in the control?
Note:
I would like to avoid declaring a bunch of DPs inside the MyUserControl
class as Foo
references multiple other types, which respectively contain properties with the same name. So to stay consistent in naming the DPs, I would have to prefix every single one with something showing the nested property they belong to.
So far, I have tried attaching DependencyProperty
s to my data model using a helper class with static
Get/Set methods as follows:
using DP = System.Windows.DependencyProperty;
static class PropertyProvider
{
public static readonly DP TextProperty = DP.RegisterAttached (
"Text",
typeof (string),
typeof (Bar),
new UIPropertyMetadata ("")
);
public static string GetText (DependencyObject obj) => (string)obj.GetValue (TextProperty);
public static void SetText (DependencyObject obj, string value) => obj.SetValue (TextProperty, value);
}
This results in an XAML Binding Failure and also - after some reasearch - seems to be the wrong approach; I understood it thus far, DPs are supposed to be attached to the control instead instead of the data/view model.
That however leaves me questioning how the data from the control is supposed to be written to/read from the context object, if the DP were attached to the control.
Another thing I attempted, was to simply set the Foo
instance as the MyUserControl
's DataContext
, and declaring the binding like this: Text="{Binding Data.Text}"
, which seemed to work here.
This also resulted in an XAML Binding Failure, because "Text property not found on object of type Foo".