I have custom control:
public class TestTextBox : TextBox
{
public TestTextBox()
{
Text = "ctor text";
}
}
And xaml that uses this control:
<StackPanel Orientation="Vertical">
<!-- 1. Use TestTextBox directly -->
<controls:TestTextBox Text="xaml text"/>
<!-- 2. Use TestTextBox in DataTemplate -->
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:TestTextBox Text="xaml text"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<System:String>111</System:String>
</ItemsControl>
<StackPanel>
The result is TestTextBox.Text is different in these cases – “xaml text” in first case, “ctor text” in second case.
Could someone explain why it works this way? I’d expect that TestTextBox.Text will be “xaml text” in both cases.
I think you need to understand Dependency Property Value Precedence.
When you are using Templates, value precedence for dependency properties is different.