Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a cell object's property to a DataGridCell in WPF DataGrid
    text
    copied!<p>Using the WPF DataGrid I have the need to change various display and related properties of a DataGridCell - such as Foreground, FontStyle, IsEnabled and so on - based on the relevant value of the cell object property. </p> <p>Now this is easy to do in code, for example (using an Observable Collection of ObservableDictionaries):</p> <pre><code> var b = new Binding("IsLocked") { Source = row[column], Converter = new BoolToFontStyleConverter() }; cell.SetBinding(Control.FontStyleProperty, b); </code></pre> <p>and works fine, however I cannot see how to do this in XAML since I can find no way to set <strong>Path</strong> to a cell object's property.</p> <p>One XAML attempts is:</p> <pre><code>&lt;Setter Property="FontStyle"&gt; &lt;Setter.Value&gt; &lt;MultiBinding Converter="{StaticResource IsLockedToFontStyleConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged"&gt; &lt;Binding /&gt; &lt;Binding RelativeSource="{x:Static RelativeSource.Self}"/&gt; &lt;/MultiBinding&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; </code></pre> <p>but there is no binding to the <em>IsLocked</em> property</p> <pre><code>public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var row = (RowViewModel) values[0]; var cell = (DataGridCell) values[1]; if (cell != null &amp;&amp; row != null) { var column = DataGridMethods.GetColumn(cell); return row[column].IsLocked ? "Italic" : "Normal"; } return DependencyProperty.UnsetValue; } </code></pre> <p>Please note that a previous version returned <em>row[col].IsLocked</em> and set the FontStyle using a DataTrigger but a returned object is not databound. </p> <p>Note, of course, that the application does not know what the columns are at design time. </p> <p>Finally DataTable's are far too inefficient for my requirements but I would be interested to see how this is done with DataTables anyway, if there is such a solution for them, this might be useful elsewhere (although I prefer using collections).</p> <p>Surely this is a common issue and I am a WPF noobie trying to go all MVVM on my project, but this issue is holding me back with respect to using the WPF DataGrid.</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