Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well here is the simplest solution I have found. (Actually I had it before I posted this and the other question but was embarrased at such a solution.Since have heard nothing else here and just it is in case anyone else is faced with the same problem, I thought I would share it.) </p> <p>Put a reference to the cell object in the DataGridCell Tag property. I do this with a combination of XAML and a code binding inside a converter as follows:</p> <pre><code> &lt;Setter Property="Tag"&gt; &lt;Setter.Value&gt; &lt;MultiBinding Converter="{StaticResource CellViewModelToTagConverter}" 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>and</p> <pre><code> public class CellViewModelToTagConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var row = values[0] as RowViewModel; var cell = values[1] as DataGridCell; if (row != null &amp;&amp; cell != null) { var column = DataGridMethods.GetColumn(cell); // hack within hack!!! (using tag way is itself a hack?) var b = new Binding("Self") {Source = row[column]}; cell.SetBinding(FrameworkElement.TagProperty, b); //... //return row[column]; return DependencyProperty.UnsetValue; } return DependencyProperty.UnsetValue; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } </code></pre> <p>You can tell what I think of this solution by my comments inside the converter.(I had to add a Self property to the Cell object and make Self=this in the constructor).</p> <p>Still it enables my Datagrid coding to be entirely MVVM - if you accept that what I have done inside the converter is consistent with MVVM. Anyway it works!</p> <p>So doing it this way I can see and manage everything from XAML such as control such binding only on certain columns by placing the XAML within the relevant column cellstyles (that is not doing this via DataGrid.CellStyle).</p> <p>Anyway, an example of usage is </p> <pre><code>&lt;Style.Triggers&gt; &lt;DataTrigger Value="true" Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag.IsLocked}"&gt; &lt;Setter Property="FontStyle" Value="Italic"/&gt; &lt;Setter Property="IsEnabled" Value="False"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; </code></pre> <p>On the XAML level it is both simple and IMHO elegant (especially for various ToolTips and Popups for which I make heavy usage of cell object's properties). However I am sure there is a better way of doing this, is there?</p> <p>Hopefully this all goes away when I can use Net 4.0 and dynamic objects, but for this project I cannot.</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