【问题标题】:Why can't I use DynamicResource with DataGridColumn.CellStyle为什么我不能将 DynamicResource 与 DataGridColumn.CellStyle 一起使用
【发布时间】:2013-07-02 17:48:51
【问题描述】:

因此,例如,我有一些具有简单模型的 MVVM WPF 应用程序:

public class MyObject
{
    public string F1 { get; set; }
    public string F2 { get; set; }
}

以及创建 3 行的简单视图模型:

public class MyViewModel
{
    public ObservableCollection<MyObject> Objects { get; set; }

    public MyViewModel()
    {
        Objects = new ObservableCollection<MyObject>
            {
                new MyObject{F1 = "V1",F2 = "B1"},
                new MyObject{F1 = "V2",F2 = "B2"},
                new MyObject{F1 = "V3",F2 = "V3"}
            };
    }
}

在视图中,我有一个带有手动定义列的DataGrid,并且我为每一列设置了CellStyleWindow.Resources 块中定义的两种样式。但是对于第一列,我使用StaticResource 和第二个DynamicResource

查看 XAML:

<Window x:Class="WpfApplication12.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="WholeWindow">
<Window.Resources>
    <Style x:Key="BaseCellClass" TargetType="DataGridCell">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Window.Resources>
<Grid>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding F1}" Header="F1" CellStyle="{StaticResource BaseCellClass}" />
            <DataGridTextColumn Binding="{Binding F2}" Header="F2" CellStyle="{DynamicResource BaseCellClass}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

所以问题是:在第二列中,资源没有应用于该列。

【问题讨论】:

  • 这看起来像我预期的行为?你能澄清一下你觉得问题出在哪里吗?
  • 问题是CellStyleDependencyProperty,但是当我尝试在其中使用绑定时,它无法工作。很明显,他们成功了 DependencyProperty 是有原因的。
  • 如果将 BaseCellClass 样式移动到应用程序资源(或在初始化 Window 之前加载的其他位置)会发生什么?
  • 在 DataGrid 上使用 DynamicResources 不是一个好主意,StaticResource 仅由引用元素检索一次并用于资源的整个生命周期。每次使用引用的对象时都会获取 DynamicResource。具有许多项目的 Datagrid 的性能影响会很大,而且如果您在同一文件中声明资源,则无需使用 DynamicResource
  • 完全同意@sa_ddam213。在您的情况下,您需要使用ConvertersDynamicResource 没用。

标签: c# wpf datagrid dynamicresource datagridcolumn


【解决方案1】:

您可以为DataGridCell Style 中的属性创建资源,然后在Style 定义中将它们引用为DynamicResource

根据您的示例,它看起来像这样:

<Window.Resources>
    <SolidColorBrush x:Key="ForegroundBrush" Color="Blue"/>

    <Style x:Key="BaseCellClass" TargetType="DataGridCell">
        <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}" />
    </Style>
</Window.Resources>
<Grid>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding F1}" Header="F1" CellStyle="{StaticResource BaseCellClass}" />
            <DataGridTextColumn Binding="{Binding F2}" Header="F2" CellStyle="{StaticResource BaseCellClass}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

资源当然会位于单独的资源文件中。

【讨论】:

  • 谢谢。你的解决方案工作正常。但是在问题中我忘了说我需要一种方法来导出像这个问题中的样式:stackoverflow.com/questions/9490264/…
  • 无论如何,我会将您的答案标记为正确,并发布我自己的解决方案,我为此使用的服务很少
【解决方案2】:

我找到了使用一点服务的解决方案。简而言之,我在 xaml 中编写了这段代码:

<wpfApplication12:DataGridColumnDynamicStyleService TargetGrid="{Binding ElementName=Grid}">
        <wpfApplication12:DataGridColumnDynamicStyleService.ColumnStyles>
            <wpfApplication12:DataGridColumnStyleBinding ColumnTag="C1" DynamicStyle="{DynamicResource BaseCellClass}" />
        </wpfApplication12:DataGridColumnDynamicStyleService.ColumnStyles>
    </wpfApplication12:DataGridColumnDynamicStyleService>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding ElementName=WholeWindow, Path=ViewModel.Objects}" x:Name="Grid">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding F1}" Header="F1" wpfApplication12:DataGridColumnDynamicStyle.ColumnTag="C1" />
            <DataGridTextColumn Binding="{Binding F2}" Header="F2" wpfApplication12:DataGridColumnDynamicStyle.ColumnTag="C2" />
        </DataGrid.Columns>
    </DataGrid>

在这里,如您所见,我使用附加属性 ColumnTag 来标识列。我创建了一个定义列样式的服务控件,并将目标数据网格设置为TargetGrid 如果你想查看所有代码,这里是google drive解决方案的链接

【讨论】:

    猜你喜欢
    • 2013-12-30
    • 2011-06-15
    • 2020-08-26
    • 2015-11-08
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2011-06-05
    相关资源
    最近更新 更多