DataGrid样式

时间:2012-12-10 16:50:23

标签: wpf xaml datagrid styles

vS 2008安装并引用了WPF工具包。 在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>

1 个答案:

答案 0 :(得分:0)

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

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

即可