自定义数据网格列标题
我正在尝试创建一个带有图像的自定义列标题的数据网格。 问题是绑定无法正常工作。
<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>
出于测试目的,我用纯文本块替换了图像控件。
编辑: 数据网格数据上下文设置为数据表
有人可以帮助我吗?
谢谢。
I'm Trying to create a datagrid with custom column headers with images.
The problem is that the binding is not working properly.
<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>
For testing purposes i replaced the image control with a plain textblock.
EDIT:
The datagrid datacontext is set to a datatable
Can anybody help me?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论