I have a data template for a type that defines a bunch of data entry fields, all with similar settings on the textboxes, something like this:
<DataTemplate x:Key="ContactInfo">
<DockPanel>
<HeaderedContentControl Header="Contact Name">
<TextBox Width="200" Text="{Binding Name, ValidatesOnDataErrors=True}"/>
</HeaderedContentControl>
<HeaderedContentControl Header="Contact Quest">
<TextBox Width="200" Text="{Binding Quest, ValidatesOnDataErrors=True}"/>
</HeaderedContentControl>
<HeaderedContentControl Header="Contact Favorite Color">
<TextBox Width="200" Text="{Binding Color, ValidatesOnDataErrors=True}"/>
</HeaderedContentControl>
</DockPanel>
</DataTemplate>
I would like to reduce duplication as much as possible and eliminate as many of the duplicated properties as I can. I know that I can get rid of the repeated Width attribute by adding
<DataTemplate.Resources>
<Style TargetType="x:Type TextBox">
<Setter Property="Width" Value="200"/>
</Style>
</DataTemplate.Resources>
to the DataTemplate. However, I also want to get rid of the redundant "ValidatesOnDataErrors=True" setting.
I have tried modifying the style to set it thusly:
<DataTemplate.Resources>
<Style TargetType="x:Type TextBox">
<Setter Property="Width" Value="200"/>
<Setter Property="Text.Binding.ValidatesOnDataErrors" Value="True"/>
</Style>
</DataTemplate.Resources>
but at compile time it complains about ValidatesOnDataErrors not being resolvable.
How does one accomplish what I want? I have upwards of 50 fields to style, and I don't want to change all of them one by one if I decide to change my validation technique.