我已将一个对象与
我正在寻找 MyObject.IsEnabled 属性,并想在下面代码中标有 ??? 的 Path-Property 中引用它。 有什么建议吗?
我们需要在DataGrid的每个单元格上设置这个属性。我没有找到一个好的XAML解决方案,所以现在我在检索信息之前在转换器中设置它。(欢迎提出改进建议) 转换器:
现在我们可以在XAML中定义全局的
DataGridTextColumn
绑定,并希望从相应的 CellStyle 中引用其属性之一。我认为这个列的每个单元格都将包含 MyObject
的一个实例。然而,我在 DataGridCell
中找不到对该对象的引用(我使用了一个简单的转换器设置断点并搜索了 DataGridCell
对象很长时间)。我正在寻找 MyObject.IsEnabled 属性,并想在下面代码中标有 ??? 的 Path-Property 中引用它。 有什么建议吗?
<DataGridTextColumn Binding="{Binding MyObject}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Path="???" Binding="{Binding RelativeSource={RelativeSource Self}, PresentationTraceSources.TraceLevel=High,Converter={StaticResource debugger}}" Value="False">
<!-- some setters -->
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
编辑:
由于我希望将这种样式应用于后来的所有DataGrid单元格,因此通过RelativeSource
找到绑定到单元格的对象而不是向MyObject
添加硬编码绑定是至关重要的。
解决方案:
感谢antiocol的建议,我能够找到适用于我的情况的解决方案,该解决方案可能可用于类似的问题。
由于问题在于我们无法从CellStyle
内部访问单元格或CellModel
的值,因此我们在DataGridCell
上使用一个附加属性将整个CellModel
存储在其中。从那里,我们可以将DataGridCell
的任何可访问属性绑定到我们CellModel
的任何属性。
附加属性的代码:
public static class DataGridUtils
{
public static CellModel GetCellModel(DependencyObject obj)
{
return (CellModel)obj.GetValue(CellModelProperty);
}
public static void SetCellModel(DependencyObject obj, CellModel value)
{
obj.SetValue(CellModelProperty, value);
}
public static readonly DependencyProperty CellModelProperty =
DependencyProperty.RegisterAttached("CellModel", typeof(CellModel), typeof(DataGridUtils), new UIPropertyMetadata(null));
我们需要在DataGrid的每个单元格上设置这个属性。我没有找到一个好的XAML解决方案,所以现在我在检索信息之前在转换器中设置它。(欢迎提出改进建议) 转换器:
public class CellToEnabledConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var cell = values[0] as DataGridCell;
DataGridTextColumn column = cell.Column as DataGridTextColumn;
//you should probably check if column is null after casting.
Binding b = column.Binding as Binding;
//Any suggestions on how to create a Binding to the parent object properly?
//I needed this workaround since I bind `MyObject.Value` to the `DataGridTextColumn`,
//but need a reference to `MyObject` here.
Binding b1 = new Binding(b.Path.Path.Split('.')[0]){ Source = cell.DataContext };
cell.SetBinding(DataGridUtils.CellModelProperty, b1);
CellModel c = DataGridUtils.GetCellModel(cell);
return c.IsEnabled;
}
现在我们可以在XAML中定义全局的
Style
,并将其应用于整个DataGrid
而不是单个列。<Window.Resources>
<converter:CellToEnabledConverter x:Key="CellToEnabledConverter" />
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Value="False">
<DataTrigger.Binding>
<!--This Converter works only on DataGridTextColumns with this minimal example!-->
<Binding Converter="{StaticResource CellToEnabledConverter}">
<Binding RelativeSource="{RelativeSource Self}" />
</Binding>
</DataTrigger.Binding>
<!--Setters-->
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<DataGrid CellStyle="{StaticResource DataGridCellStyle}">
<DataGridTextColumn Binding="{Binding MyObject.Value}"/>
</DataGrid>
我发现网上有几条评论声称“使用当前的DataGrid
无法根据其值设置单元格样式”,希望这个解决方法能对某些人有所帮助。
ItemsSource
是DataGrid
中的什么? - DennisDataContext
是整个RowModel
。恐怕您无法以这种方式获取CellModel
。 - Dennis<DataGridTextColumn Binding="{Binding MyObject}">
将整个对象绑定到了我的GridCell上。这个对象实际上绑定在哪里?我浏览了大部分的可视树(包括DataGrid、DataGridView、DataGridCellsPresenter、DataGridCell),但找不到MyObject
绑定到的属性。GUI仍然显示我的对象的正确ToString()
值,所以引用必须在某个地方! - H WRowModel
。当 DG 必须呈现行时,它会查找特定列的绑定表达式,并解析其值以在单元格中呈现。解析不会更改单元格的数据上下文。 - Dennis