Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I set a binding property via a style in XAML?
    text
    copied!<p>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:</p> <pre><code>&lt;DataTemplate x:Key="ContactInfo"&gt; &lt;DockPanel&gt; &lt;HeaderedContentControl Header="Contact Name"&gt; &lt;TextBox Width="200" Text="{Binding Name, ValidatesOnDataErrors=True}"/&gt; &lt;/HeaderedContentControl&gt; &lt;HeaderedContentControl Header="Contact Quest"&gt; &lt;TextBox Width="200" Text="{Binding Quest, ValidatesOnDataErrors=True}"/&gt; &lt;/HeaderedContentControl&gt; &lt;HeaderedContentControl Header="Contact Favorite Color"&gt; &lt;TextBox Width="200" Text="{Binding Color, ValidatesOnDataErrors=True}"/&gt; &lt;/HeaderedContentControl&gt; &lt;/DockPanel&gt; &lt;/DataTemplate&gt; </code></pre> <p>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</p> <pre><code>&lt;DataTemplate.Resources&gt; &lt;Style TargetType="x:Type TextBox"&gt; &lt;Setter Property="Width" Value="200"/&gt; &lt;/Style&gt; &lt;/DataTemplate.Resources&gt; </code></pre> <p>to the DataTemplate. However, I also want to get rid of the redundant "ValidatesOnDataErrors=True" setting.</p> <p>I have tried modifying the style to set it thusly:</p> <pre><code>&lt;DataTemplate.Resources&gt; &lt;Style TargetType="x:Type TextBox"&gt; &lt;Setter Property="Width" Value="200"/&gt; &lt;Setter Property="Text.Binding.ValidatesOnDataErrors" Value="True"/&gt; &lt;/Style&gt; &lt;/DataTemplate.Resources&gt; </code></pre> <p>but at compile time it complains about ValidatesOnDataErrors not being resolvable.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload