自定义Datagrid列标题

时间:2011-12-19 10:13:05

标签: wpf data-binding datagrid controltemplate

我正在尝试使用包含图片的自定义列标题创建数据网格。 问题是绑定无法正常工作。

<Window.Resources>
    <DataTemplate x:Key="header">
        <Image Source="{Binding ColumnHeader}"/>
    </DataTemplate>

    <DataGridRowHeader x:Key="Row"/>
    <DataTemplate x:Key="RowHeaderTemplate">
        <Grid>
            <TextBlock TextWrapping="Wrap" Text="Text"/>
        </Grid>
    </DataTemplate>
    <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
        <Setter Property="Width" Value="8"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeWE"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Microsoft_Windows_Themes:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" IsClickable="{TemplateBinding CanUserSort}" IsPressed="{TemplateBinding IsPressed}" IsHovered="{TemplateBinding IsMouseOver}" Padding="{TemplateBinding Padding}" SortDirection="{TemplateBinding SortDirection}" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" ThemeColor="NormalColor">
                            <TextBlock Text="{Binding Path=ColumnHeader}"/>
                        </Microsoft_Windows_Themes:DataGridHeaderBorder>
                        <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                        <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{StaticResource ColumnHeaderGripperStyle}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </Window.Resources>
<Grid x:Name="LayoutRoot">
    <DataGrid x:Name="dataGrid" ItemsSource="{Binding}" RowHeaderTemplate="{DynamicResource RowHeaderTemplate}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"/>
</Grid>

出于测试目的,我用纯文本块替换了图像控件。

编辑: datagrid datacontext设置为数据表

有人能帮助我吗?

感谢。

0 个答案:

没有答案