views:

113

answers:

1

Using Release WPF DataGrid I am trying to bind to the property of a CellViewModel (that supports INotifyPropertyChanged of course). I am binding the DataGrid's ItemsSource to an ObservableCollection of type RowViewModel (which is inherited from Dr.WPF's ObservableDictonary) of type CellViewModel and I want bind to a property of a CellViewModel.

For example in what follows I am trying to change the state of the DatagridCell.FontStyle property based on my CellViewModel.IsLocked boolean property (so that if IsLocked then Italic).

First to get expose the relevant CellViewModelobject in XAML

<DataGrid.Resources>

       <cv:RowColumnToCellConverter x:Key="RowColumnToCellConverter"/>

        <MultiBinding x:Key="CellViewModel" Converter="{StaticResource RowColumnToCellConverter}">
            <Binding />
            <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Column" />
        </MultiBinding>

</DataGrid.Resources>

The RowColumnToCellConverter is

public class RowColumnToCellConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   {
        RowViewModel<string,CellViewModel> row = values[0] as RowViewModel<string,CellViewModel>;
        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();
    }
 }

Then create the DataTrigger in XAML:

    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Value="True" >
                    <DataTrigger.Binding >
                          <Binding Path="isLocked" Source="{StaticResource CellViewModel}" />
                    </DataTrigger.Binding>
                    <Setter Property="FontStyle" Value="Italic"/>
                </DataTrigger>
            </Style.Triggers>             
        </Style>
    </DataGrid.CellStyle>

Now this does not work but is an example of what I would like to do, hopefully it is not far off. Certainly the MultiBinding is never set here (since the Converter is never called checking in debug).

Elsewhere the MultiBinding/MultiConverter works (although I cut it down for this post hopefully did not introduce any errors) that is as a part of a DataTrigger /MultiTrigger (not as a StaticResource as in the above - which is what I would like)) but then it only reveals an object that is not iself bound to.

That is I could have a variant of converter where the object returned is row[col].Islocked however then the Islocked Property is not registered for PropertyChange notifications.

Now I cannot add the Islocked 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 {Bind Path=IsLocked} to refer to.

So I tried nesting MultiBinding in something like the following which does not work since BindingCollection only accepts Binding objects not MultBinding objects.

   <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger>
                     <DataTrigger.Binding>
                        <MultiBinding>
                            <MultiBinding Converter="{StaticResource RowColumnToCellConverter}">
                                <Binding />
                                <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Column" />
                            </MultiBinding>
                            <Binding Path="IsLocked"/>
                        </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter Property="FontStyle" Value="Italic"/>
                </DataTrigger>
            </Style.Triggers>              
        </Style>
    </DataGrid.CellStyle>

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).

A: 

The answer here is the same as I have provided in my other similar question

Martino