Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF DataGrid: How do I access the DataRow a TextBox etc. resides in?
    text
    copied!<p>So I have columns defined like this:</p> <pre><code> &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBox Text="{Binding Path=COLUMN_NAME, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; </code></pre> <p>TextBox isn't the only control that may exist in the DataTemplate for a column, I have DateTimePickers, ComboBoxes, etc.</p> <p>And what I want to do is define some style triggers like this that access some property in the DataRow:</p> <pre><code> &lt;Style TargetType="{x:Type TextBox}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Row.RowState}" Value="Modified"&gt; &lt;Setter Property="Foreground" Value="LightGreen" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>Now unfortunately it would appear the TextBoxes are never logical children of DataGridRows. So what is the solution? Of course I can create a trigger that targets DataGridRows themselves, but this is redundant because setting the foreground property will do nothing (TextBoxes and other controls sit in front).</p> <p>Any help most appreciated. Fugu</p> <p>EDIT: Here is the solution I chose, because RowState does not notify its listeners and extending DataRow is problematic.</p> <p>1) Bind an event to the control(s) for when their data changes. 2) Refresh the Style to check for the RowState again:</p> <pre><code> Style s = ((TextBox)sender).Style; ((TextBox)sender).Style = null; ((TextBox)sender).Style = s; </code></pre> <p>Obviously it will be made more generic than that.</p> <p>edit 2: This blatantly won't work as I'd need to reset the style for each control individually, which even if it was possible would be a bad thing to do</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