使用 Release WPFDataGrid
我试图绑定到 a 的属性CellViewModel
(当然支持INotifyPropertyChanged
)。我将DataGrid
's绑定ItemsSource
到ObservableCollection
类型的类型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 的一个非常基本的要求,所以我希望答案非常简单)。