【问题标题】:DataGrid styles数据网格样式
【发布时间】:2012-12-10 16:50:23
【问题描述】:

安装并引用了带有 WPF 工具包的 vS 2008。 在 Window1.xaml 我添加了这一行:

xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"

它运行,网格显示数据,直到我尝试设置网格样式。尝试应用使文本居中的样式时出现错误。错误是指 App.xaml 并且是:

类型引用找不到名为“DataGridCell”的公共类型。第 9 行位置 75。

我的 App.xaml

<Application x:Class="DataGridStyles.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                        
StartupUri="Window1.xaml">
<Application.Resources>

    <Style x:Key="CenterCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" 
                                  VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="Foreground" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>

</Application.Resources>
</Application>

【问题讨论】:

  • 我遇到了同样的问题,如果你找到了,请发布解决方案。

标签: wpf xaml datagrid styles


【解决方案1】:

如果数据网格是 WPF 工具包的一部分,您还需要在 App.xaml 中添加该命名空间 (xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit")。

然后,只需将您的 TargetType="{x:Type DataGridCell}" 更改为 TargetType="{x:Type my:DataGridCell}"

【讨论】:

    猜你喜欢
    • 2010-10-07
    • 2014-06-11
    • 1970-01-01
    • 2014-05-06
    • 2012-09-14
    • 2011-04-18
    相关资源
    最近更新 更多