I have some strange problem with DependecyProperty-binding. To keep the question simpler i´ve created some dummy-control, that has the same unwanted behaviour
I have a UserControl, that has a DependencyProperty defined in code behind:
public static readonly DependencyProperty TestValueProperty = DependencyProperty.Register("TestValue", typeof(string), typeof(Test), new PropertyMetadata(default(string)));
public string TestValue
{
get { return (string)GetValue(TestValueProperty); }
set { SetValue(TestValueProperty, value); }
}
This property is used in XAML:
<Label Content="{Binding TestValue}" />
This control should be used in another control like this:
<views:Test TestValue="{Binding Settings.Setting123}" />
Settings
is defined in viewmodel as property.
But the content of Settings.Setting123
is not visible in my usercontrol´s label.
When writing some fixes value instead of the binding it works fine:
<views:Test TestValue="Test" />
But of course i do not want a fixed value, but the content of the bound object. Any hint what is going wrong here?
Thanks in advance!
You didn't share enough code for anybody to recreate the issue, but reading between the lines, I'm guessing that Label
is in your UserControl
XAML. If TestValue
is a property of your UserControl
, this will probably work:
<Label Content="{Binding TestValue, RelativeSource={RelativeSource AncestorType=UserControl}}" />
One thing you did right: Your UserControl constructor must always set DataContext = this;
. No exceptions.