Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a Property of an object in Item (row) to a DataGridCell Property in WPF DataGrid
    primarykey
    data
    text
    <p>Using Release WPF <code>DataGrid</code> I am trying to bind to the property of a <code>CellViewModel</code> (that supports <code>INotifyPropertyChanged</code> of course). I am binding the <code>DataGrid</code>'s <code>ItemsSource</code> to an <code>ObservableCollection</code> of type <code>RowViewModel</code> (which is inherited from Dr.WPF's <code>ObservableDictonary</code>) of type <code>CellViewModel</code> and I want bind to a property of a <code>CellViewModel</code>. </p> <p>For example in what follows I am trying to change the state of the <code>DatagridCell.FontStyle</code> property based on my <code>CellViewModel.IsLocked</code> boolean property (so that if <code>IsLocked</code> then Italic). </p> <p>First to get expose the relevant CellViewModelobject in XAML:</p> <pre><code>&lt;DataGrid.Resources&gt; &lt;cv:RowColumnToCellConverter x:Key="RowColumnToCellConverter"/&gt; &lt;MultiBinding x:Key="CellViewModel" Converter="{StaticResource RowColumnToCellConverter}"&gt; &lt;Binding /&gt; &lt;Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Column" /&gt; &lt;/MultiBinding&gt; &lt;/DataGrid.Resources&gt; </code></pre> <p>The RowColumnToCellConverter is</p> <pre><code>public class RowColumnToCellConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { RowViewModel&lt;string,CellViewModel&gt; row = values[0] as RowViewModel&lt;string,CellViewModel&gt;; string column = (values[1] as DataGridColumn).SortMemberPath; string col = column.Substring(1, column.Length - 2); return row[col]; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } </code></pre> <p>Then create the <code>DataTrigger</code> in XAML:</p> <pre><code> &lt;DataGrid.CellStyle&gt; &lt;Style TargetType="{x:Type DataGridCell}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Value="True" &gt; &lt;DataTrigger.Binding &gt; &lt;Binding Path="isLocked" Source="{StaticResource CellViewModel}" /&gt; &lt;/DataTrigger.Binding&gt; &lt;Setter Property="FontStyle" Value="Italic"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/DataGrid.CellStyle&gt; </code></pre> <p>Now this does not work but is an example of what I would <em>like</em> to do, hopefully it is not far off. Certainly the <code>MultiBinding</code> is never set here (since the <code>Converter</code> is never called checking in debug). </p> <p>Elsewhere the <code>MultiBinding/MultiConverter</code> works (although I cut it down for this post hopefully did not introduce any errors) that is as a part of a <code>DataTrigger/MultiTrigger</code> (not as a StaticResource as in the above - which is what I would <em>like</em>)) but then it only reveals an object that is not iself bound to. </p> <p>That is I could have a variant of converter where the object returned is <em>row[col].Islocked</em> however then the Islocked Property is not registered for PropertyChange notifications. </p> <p>Now I cannot add the <code>IsLocked</code> property (or can I?) as a third bind statement for the BindingCollection in the original MultiBind, as until the MultiBind returns an object, there is nothing for the third bind statement <em>{Bind Path=IsLocked}</em> to refer to. </p> <p>So I tried nesting <code>MultiBinding</code> in something like the following which does not work since <code>BindingCollection</code> only accepts Binding objects not <code>MultBinding</code> objects.</p> <pre><code> &lt;DataGrid.CellStyle&gt; &lt;Style TargetType="{x:Type DataGridCell}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger&gt; &lt;DataTrigger.Binding&gt; &lt;MultiBinding&gt; &lt;MultiBinding Converter="{StaticResource RowColumnToCellConverter}"&gt; &lt;Binding /&gt; &lt;Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Column" /&gt; &lt;/MultiBinding&gt; &lt;Binding Path="IsLocked"/&gt; &lt;/MultiBinding&gt; &lt;/DataTrigger.Binding&gt; &lt;Setter Property="FontStyle" Value="Italic"/&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/DataGrid.CellStyle&gt; </code></pre> <p>Hopefully my problem should be clear from the above even if my attempted solutions are far off. So what am I missing? (Probably a lot since I have only been using WPF/MVVM for a few weeks and this is a really basic requirement for working with any DataGrid, so I hope the answer is embarrassingly simple).</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.
 

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