绑定单元格对象';WPF DataGrid中的DataGridCell的属性
使用WPF DataGrid,我需要根据单元格对象属性的相关值更改DataGridCell的各种显示和相关属性,如前台、FontStyle、IsEnabled等 现在,这在代码中很容易做到,例如(使用ObservableDictionary的可观察集合): 工作正常,但是我看不到如何在XAML中实现这一点,因为我找不到方法将路径设置为单元格对象的属性 一个XAML尝试是:绑定单元格对象';WPF DataGrid中的DataGridCell的属性,wpf,data-binding,mvvm,datagrid,datagridcell,Wpf,Data Binding,Mvvm,Datagrid,Datagridcell,使用WPF DataGrid,我需要根据单元格对象属性的相关值更改DataGridCell的各种显示和相关属性,如前台、FontStyle、IsEnabled等 现在,这在代码中很容易做到,例如(使用ObservableDictionary的可观察集合): 工作正常,但是我看不到如何在XAML中实现这一点,因为我找不到方法将路径设置为单元格对象的属性 一个XAML尝试是: <Setter Property="FontStyle"> <Setter.Value>
<Setter Property="FontStyle">
<Setter.Value>
<MultiBinding Converter="{StaticResource IsLockedToFontStyleConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
<Binding />
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
请注意,以前的版本返回了行[col]。已锁定,并使用DataTrigger设置FontStyle,但返回的对象不是数据绑定的
当然,请注意,应用程序在设计时不知道列是什么
最后,对于我的需求来说,DataTable的效率太低了,但我想看看DataTables是如何实现的,如果有这样的解决方案,这可能在其他地方很有用(尽管我更喜欢使用集合)
当然,这是一个常见的问题,我是一个WPF noobie,试图在我的项目中使用所有MVVM,但这个问题阻碍了我使用WPF数据网格。这是我找到的最简单的解决方案。(事实上,我在发布这个和另一个问题之前就有过这个问题,但对于这样的解决方案感到尴尬。因为我在这里没有听到任何其他问题,只是为了防止其他人面临同样的问题,我想我会分享它。) 在DataGridCell标记属性中放置对单元格对象的引用。我使用XAML和转换器内部的代码绑定的组合来实现这一点,如下所示:
<Setter Property="Tag">
<Setter.Value>
<MultiBinding Converter="{StaticResource CellViewModelToTagConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
<Binding />
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
您可以通过我在转换器中的注释来了解我对这个解决方案的看法(我必须向Cell对象添加Self属性,并在构造函数中设置Self=this)
不过,它使我的Datagrid编码完全成为MVVM——如果您接受我在转换器中所做的与MVVM一致的话。不管怎样,它是有效的
因此,通过这种方式,我可以查看和管理XAML中的所有内容,例如,通过将XAML放在相关列cellstyles中(而不是通过DataGrid.CellStyle)来控制仅对特定列的绑定
无论如何,用法的一个例子是
<Style.Triggers>
<DataTrigger Value="true" Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag.IsLocked}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
在XAML级别上,它既简单又优雅(特别是对于各种工具提示和弹出窗口,我大量使用了cell对象的属性)。然而,我相信有更好的方法可以做到这一点,是吗
希望当我可以使用Net4.0和dynamic objects时,这一切都会消失,但对于这个项目,我无法做到。谢谢分享。这很难看,但这也是迄今为止我看到的解决这个问题的最简单的方法……我意识到这是一篇老文章,但我发现我可以简单地将
返回行[column]如果..中的代码>。。块和它的工作。上面的代码有什么不同之处(即创建绑定)?除了在转换器中,绑定不会发生。如果不在转换器内绑定,只返回row[column],然后更新引用的单元格,则不会绑定,并且在DataGrid中看不到更新。我有一个更好的非黑客解决方案,尽管XAML更复杂。当我有时间的时候,我会把它添加到这里。我会对这个解决方案感兴趣:)
<Setter Property="Tag">
<Setter.Value>
<MultiBinding Converter="{StaticResource CellViewModelToTagConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
<Binding />
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
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 && 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();
}
}
<Style.Triggers>
<DataTrigger Value="true" Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag.IsLocked}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>