views:

30

answers:

1

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.

A: 

May be this will be helpfull.

Eugene Cheverda
It's helpful in that it is exactly the problem I'm seeing, it's unhelpful in that I hoped there would be an easier solution. If nothing better comes along, I'll probably use this approach.Unfortunately, up-voting this answer requires more reputation than I have, otherwise I would.
Blaise Pascal