1

使用 Release WPFDataGrid我试图绑定到 a 的属性CellViewModel(当然支持INotifyPropertyChanged)。我将DataGrid's绑定ItemsSourceObservableCollection类型的类型RowViewModel(继承自 Dr.WPF's ObservableDictonary),CellViewModel并且我想绑定到 a 的属性CellViewModel

例如,在接下来的内容中,我试图DatagridCell.FontStyle根据我的CellViewModel.IsLocked布尔属性更改属性的状态(这样如果是IsLocked斜体)。

首先要在 XAML 中公开相关的 CellViewModel 对象:

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

RowColumnToCellConverter 是

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();
    }
 }

然后DataTrigger在 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>

现在这不起作用,但这是我想做的一个例子希望它不会太远。当然,MultiBinding这里永远不会设置(因为Converter永远不会调用检查调试)。

在其他地方的MultiBinding/MultiConverter作品(虽然我为这篇文章减少了它希望没有引入任何错误)作为 a 的一部分DataTrigger/MultiTrigger(不是像上面那样作为 StaticResource - 这是我想要))但它只揭示了一个不受自身约束的对象。

也就是说,我可以有一个转换器的变体,其中返回的对象是row[col].Islocked但是 Islocked 属性未注册用于 PropertyChange 通知。

现在我不能将IsLocked属性(或者我可以吗?)添加为原始 MultiBind 中 BindingCollection 的第三个绑定语句,因为在 MultiBind 返回一个对象之前,第三个绑定语句{Bind Path=IsLocked}没有任何内容可以引用.

所以我尝试嵌套MultiBinding在类似下面的东西中,它不起作用,因为BindingCollection只接受绑定对象而不是MultBinding对象。

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

希望我的问题应该从上面清楚,即使我尝试的解决方案还很遥远。那么我错过了什么?(可能很多,因为我只使用 WPF/MVVM 几个星期,这是使用任何 DataGrid 的一个非常基本的要求,所以我希望答案非常简单)。

4

0 回答 0