Note that there are some explanatory texts on larger screens.

plurals
  1. POStyle within DataTemplate is only being applied to the last item in ItemsControl?
    primarykey
    data
    text
    <p>In the XAML below, I have an ItemsControl that has three DataObjects.<br> I use a DataTemplate to display DataObjects as Buttons with an "X" on them.<br> The Button uses a Style to set its Content. </p> <p>If the Setter.Value is "X", everything works great!<br> However, if I change the Setter.Value to a TextBlock whose TextProperty is "X", <strong>the X only appears on the last Button</strong> (the third DataObject) and the first two Buttons are empty. </p> <p><strong>Is this a bug, or can anybody explain why this happens?</strong></p> <p>Note 1) This is a contrived example to isolate the problem being encountered.<br> Note 2) I've put both Setter.Value options in the code so you can reproduce both the successful and unsuccessful cases just by having one of them commented out.<br> Note 3) It appears, this problem is specific to Setters for the 'Content' property. If I use a Setter for the Background property, it correctly applies to all of the DataObjects.</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;Style x:Key="myButtonStyle" TargetType="{x:Type Button}"&gt; &lt;Setter Property="Content"&gt; &lt;!--&lt;Setter.Value&gt;X&lt;/Setter.Value&gt;--&gt; &lt;Setter.Value&gt;&lt;TextBlock Text="X" /&gt;&lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;Setter Property="Background"&gt; &lt;Setter.Value&gt; &lt;SolidColorBrush Color="Red" /&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;ItemsControl&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type DataObject}"&gt; &lt;Button Height="24" Width="24" Style="{StaticResource myButtonStyle}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;ItemsControl.Items&gt; &lt;DataObject /&gt; &lt;DataObject /&gt; &lt;DataObject /&gt; &lt;/ItemsControl.Items&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; </code></pre> <p><H3>Solution:</H3> Unfortunately, I still cannot explain why the 'Content' Setter fails to work on all but the last DataObject when the Content is set to be a control such as a TextBlock rather than straight text.</p> <p>However, Dmitry's suggestion of using setting the 'ContentTemplate' instead of 'Content' is a very acceptable workaround that still allows for a re-usable Style.</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;DataTemplate x:Key="textBlockWithX"&gt; &lt;TextBlock Text="X" /&gt; &lt;/DataTemplate&gt; &lt;Style x:Key="myButtonStyle" TargetType="{x:Type Button}"&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource textBlockWithX}" /&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;ItemsControl&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type DataObject}"&gt; &lt;Button Height="24" Width="24" Style="{StaticResource myButtonStyle}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;ItemsControl.Items&gt; &lt;DataObject /&gt; &lt;DataObject /&gt; &lt;DataObject /&gt; &lt;/ItemsControl.Items&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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