Reputation: 97
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.
Upvotes: 4
Views: 237
Reputation: 526
I agree with gaurawerma.
The precedence of value set in constructor is higher than that set in Data Template. Hence you see the result as different in both the cases.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/a4e7ed36-9a8a-48ce-a5d5-00a49376669b
Upvotes: 0
Reputation: 825
It seems what you are doing it not correct. It is actualy not rendering the TextBox as you have not given items source. Or what you can do is something like
<ItemsControl.Items>
<TestTextBoxFet:TestTextBox Text="xaml text"/>
</ItemsControl.Items>
Upvotes: 1
Reputation: 1826
I think you need to understand Dependency Property Value Precedence.
When you are using Templates, value precedence for dependency properties is different.
Upvotes: 1