Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general just only apply the style where applicable. If you want implicit application use nested styles:</p> <pre><code>&lt;Style TargetType="{x:Type DataGrid}"&gt; &lt;Style.Resources&gt; &lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}" Value="True"&gt; &lt;Setter Property="Foreground" Value="White" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Style.Resources&gt; &lt;/Style&gt; </code></pre> <p>If you have other parts which you want to apply to all TextBoxes take out those parts in a serarate style and use <a href="http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx" rel="nofollow"><code>BasedOn</code></a> in the style which applies to the TextBoxes inside the DataGrid.</p> <hr> <p><strong>Edit:</strong> MultiDataTrigger seems to return right away if a condition is not met so you can avoid binding errors:</p> <pre><code>&lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Resources&gt; &lt;vc:HasAncestorOfTypeConverter x:Key="HasAncestorOfTypeConverter" AncestorType="{x:Type DataGridCell}" /&gt; &lt;/Style.Resources&gt; &lt;Style.Triggers&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource HasAncestorOfTypeConverter}}" Value="True" /&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}, Path=IsSelected}" Value="True" /&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/MultiDataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <pre class="lang-cs prettyprint-override"><code>public class HasAncestorOfTypeConverter : IValueConverter { public Type AncestorType { get; set; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return false; DependencyObject current = value as DependencyObject; while (true) { current = VisualTreeHelper.GetParent(current); if (current == null) { return false; } if (current.GetType() == AncestorType) { return true; } } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotSupportedException(); } } </code></pre> <p>This of course causes quite som overhead so it might not be such a good solution, then again if the RelativeSource-binding fails it also had to go up the tree first.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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