繁体   English   中英

为什么我不能将DynamicResource与DataGridColumn.CellStyle一起使用

[英]Why can't I use DynamicResource with DataGridColumn.CellStyle

所以,例如我有一些简单模型的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 ,每列都设置了CellStyle 两种样式都在Window.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>

所以问题是:在第二列中,资源不会应用于列。

见第二栏

您可以为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>

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

我找到了使用一点服务的解决方案。 用几句话我用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驱动器上解决方案的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM