列HeaderStyle目标类型[英] Column HeaderStyle target type

问题描述

DataGridColumnHeader header = new DataGridColumnHeader();样式 headerstyle = new System.Windows.Style();headerstyle.TargetType = typeof(DataGridColumnHeader);Setter setter = new Setter();Setter setter1 = new Setter();Setter setter2 = new Setter();setter.Property = FontSizeProperty;FontSize = 10;setter.Value = 字体大小;headerstyle.Setters.Add(setter);setter1.Property = 前景属性;前景=画笔.BlueViolet;setter1.Value = 前景;headerstyle.Setters.Add(setter1);Setter setter3 = new Setter();LinearGradientBrush lb = new LinearGradientBrush();点起点 = new Point(0.5, 0);点端点 = new Point(1, 1);lb.StartPoint = 起点;lb.EndPoint = 端点;GradientStop gst = new GradientStop();lb.GradientStops.Add(new GradientStop(Colors.Black, 0.0));lb.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));lb.GradientStops.Add(new GradientStop(Colors.Green, 1.0));setter3.Property = 背景属性;setter3.Value = 磅;headerstyle.Setters.Add(setter3); dataGrid1.ColumnHeaderStyle = headerstyle;


这有什么问题.执行时会报错说目标不匹配.

DataGridColumnHeader'' TargetType 与元素"DataGridColumnHeader"的类型不匹配.

我正在附加相应的 XAML 文件

<dg:DataGrid.Columns><dg:DataGridTextColumn Header="AuthorID" FontWeight="黑色" 前景="红色"                                    FontSize="16"FontStyle="斜体"                                    CanUserReorder="True"                                    CanUserSort="True"SortDirection="升序" 绑定="{Binding Path=AuthorID}"><dg:DataGridTextColumn.ElementStyle><风格 TargetType="TextBlock"> Property="TextAlignment" Value="Center"/></风格></dg:DataGridTextColumn.ElementStyle><dg:DataGridTextColumn.EditingElementStyle><风格 TargetType="TextBox"> Property="TextWrapping" Value="Wrap"/> Property="AcceptsReturn" Value="true"/></风格></dg:DataGridTextColumn.EditingElementStyle><dg:DataGridColumn.HeaderStyle><风格 TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="垫"><GradientStop Color="Bisque" Offset="0"/><GradientStop Color="Cyan" Offset="1"/></LinearGradientBrush></Setter.Value></Setter><Setter 属性="前景" ="黑色" /><Setter 属性="FontSize" ="20" /><Setter 属性="Horizo​​ntalContentAlignment" ="中心" /><Setter 属性="BorderBrush" ="黄色"/><Setter 属性="BorderThickness" ="2,2,2,2"/></Style></dg:DataGridColumn.HeaderStyle></dg:DataGridTextColumn><dg:DataGridTextColumn Header="FirstName" FontWeight="粗体" 前景="DarkGreen" FontSize="16" FontStyle="斜体"                                    CanUserSort="False"绑定="{Binding Path=FirstName}"><dg:DataGridTextColumn.ElementStyle><风格 TargetType="TextBlock"> Property="TextAlignment" Value="Center"/></风格></dg:DataGridTextColumn.ElementStyle><dg:DataGridTextColumn.EditingElementStyle><风格 TargetType="TextBox"> Property="TextWrapping" Value="Wrap"/> Property="AcceptsReturn" Value="true"/></风格></dg:DataGridTextColumn.EditingElementStyle><dg:DataGridColumn.HeaderStyle><风格 TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="垫"><GradientStop Color="Lavender" Offset="0"/><GradientStop Color="BlanchedAlmond" Offset="1"/></LinearGradientBrush></Setter.Value></Setter><Setter 属性="前景" ="黑色" /><Setter 属性="FontSize" ="20" /><Setter 属性="Horizo​​ntalContentAlignment" ="中心" /><Setter 属性="宽度" ="自动" /><Setter 属性="BorderBrush" ="黄色"/><Setter 属性="BorderThickness" ="2,2,2,2"/></Style></dg:DataGridColumn.HeaderStyle></dg:DataGridTextColumn><dg:DataGridTextColumn Header="LastName" FontWeight="粗体" 前景="OrangeRed" FontSize="16" FontStyle="斜体"                                    CanUserSort="False"绑定="{Binding Path=LastName}"><dg:DataGridTextColumn.ElementStyle><风格 TargetType="TextBlock"> Property="TextAlignment" Value="Center"/></风格></dg:DataGridTextColumn.ElementStyle><dg:DataGridTextColumn.EditingElementStyle><风格 TargetType="TextBox"> Property="TextWrapping" Value="Wrap"/> Property="AcceptsReturn" Value="true"/></风格></dg:DataGridTextColumn.EditingElementStyle><dg:DataGridColumn.HeaderStyle><风格 TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="垫"><GradientStop Color="Brown" Offset="0"/> Color="White" Offset="1"/></LinearGradientBrush></Setter.Value></Setter><Setter 属性="前景" ="黑色" /><Setter 属性="FontSize" ="20" /><Setter 属性="Horizo​​ntalContentAlignment" ="中心" /><Setter 属性="宽度" ="自动" /><Setter 属性="BorderBrush" ="黄色"/><Setter 属性="BorderThickness" ="2,2,2,2"/></Style></dg:DataGridColumn.HeaderStyle></dg:DataGridTextColumn><dg:DataGridTemplateColumn 标题="DOB" MinWidth="100" CanUserSort="False"><dg:DataGridTemplateColumn.HeaderStyle><风格 TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="1,.1" StartPoint="0.5,1" SpreadMethod="垫"> Color="Orange" Offset="1"/><GradientStop Color="Thistle" Offset="0"/></LinearGradientBrush></Setter.Value></Setter><Setter 属性="前景" ="黑色" /><Setter 属性="FontSize" ="20" /><Setter 属性="Horizo​​ntalContentAlignment" ="中心" /><Setter 属性="宽度" ="自动" /><Setter 属性="BorderBrush" ="黄色"/><Setter 属性="BorderThickness" ="2,2,2,2"/></Style></dg:DataGridTemplateColumn.HeaderStyle><dg:DataGridTemplateColumn.CellEditingTemplate><DataTemplate><dg:DatePicker SelectedDate="{Binding DOB}" SelectedDateFormat="短" CalendarStyle="DynamicResource CalenderControlTemplate" 移除="黄色" /></DataTemplate></dg:DataGridTemplateColumn.CellEditingTemplate><dg:DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding DOB, StringFormat=d}" Foreground="DarkTurquoise" Horizo​​ntalAlignment="中心" /></DataTemplate></dg:DataGridTemplateColumn.CellTemplate></dg:DataGridTemplateColumn><dg:DataGridCheckBoxColumn 标题="通过/失败" 绑定="{绑定通行证}"><dg:DataGridColumn.HeaderStyle><风格 TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="1,1" StartPoint="0.5,0" SpreadMethod="垫"><GradientStop Color="#FFC2D6F6" Offset="0"/><GradientStop Color="White" Offset="1"/></LinearGradientBrush></Setter.Value></Setter><Setter Property="Foreground" Value="Black" /><Setter Property="FontSize" Value="20" /><Setter Property="HorizontalContentAlignment" Value="Center" /><Setter Property="Width" Value="Auto" /><Setter Property="BorderBrush" Value="Yellow"/><Setter Property="BorderThickness" Value="2,2,2,2"/></Style></dg:DataGridColumn.HeaderStyle></dg:DataGridCheckBoxColumn><dg:DataGridTextColumn Header="Nationality" FontWeight="Bold" Foreground="SandyBrown"                                    FontSize="16" FontStyle="Italic" CanUserSort="False"                                    Binding="{Binding Path= Nationality}" Width="Auto"><dg:DataGridTextColumn.ElementStyle><风格 TargetType="TextBlock"> Property="TextAlignment" Value="Center"/></风格></dg:DataGridTextColumn.ElementStyle><dg:DataGridTextColumn.EditingElementStyle><风格 TargetType="TextBox"><Setter Property="TextWrapping" Value="Wrap"/><Setter Property="AcceptsReturn" Value="true"/></风格></dg:DataGridTextColumn.EditingElementStyle><dg:DataGridColumn.HeaderStyle><Style TargetType="{x:Type dg:DataGridColumnHeader}"><Setter Property="Background" ><Setter.Value><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Pad"><GradientStop Color="Blue" Offset="0"/><GradientStop Color="White" Offset="1"/></LinearGradientBrush></Setter.Value></Setter><Setter Property="Foreground" Value="Black" /><Setter Property="FontSize" Value="20" /><Setter Property="HorizontalContentAlignment" Value="Center" /><Setter Property="Width" Value="Auto" /><Setter Property="BorderBrush" Value="Yellow"/><Setter Property="BorderThickness" Value="2,2,2,2"/></Style></dg:DataGridColumn.HeaderStyle></dg:DataGridTextColumn></dg:DataGrid.Columns>


regards iqbal

推荐答案

Hi when I run your code it works perfectly. Is it not because you may be setting the style for the DatagridColumnHeader in XAML as well? And when are you initialising this code? Can you maybe attach your xaml for your datagrid control please.

Remove your namespace reference of dg: and add this xaml
<datagrid autogeneratecolumns="False" height="200" horizontalalignment="Left" margin="153,84,0,0" name="dataGrid1" verticalalignment="Top" width="200"><datagrid.columns><datagridtextcolumn header="AuthorID" fontweight="Black" foreground="Red">FontSize="16" FontStyle="Italic"CanUserReorder="True"CanUserSort="True" SortDirection="Ascending" Binding="{Binding Path=AuthorID}"><datagridtextcolumn.elementstyle><style targettype="TextBlock"><setter property="TextAlignment" value="Center"/></style></datagridtextcolumn.elementstyle><datagridtextcolumn.editingelementstyle><style targettype="TextBox"><setter property="TextWrapping" value="Wrap"/><setter property="AcceptsReturn" value="true"/></style></datagridtextcolumn.editingelementstyle><datagridcolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="1,1" startpoint="0.5,0" spreadmethod="Pad"><gradientstop color="Bisque" offset="0"/><gradientstop color="Cyan" offset="1"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="BorderBrush" value="yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridcolumn.headerstyle></datagridtextcolumn><datagridtextcolumn header="FirstName" fontweight="Bold" foreground="DarkGreen" fontsize="16" fontstyle="Italic">CanUserSort="False" Binding="{Binding Path=FirstName}"><datagridtextcolumn.elementstyle><style targettype="TextBlock"><setter property="TextAlignment" value="Center"/></style></datagridtextcolumn.elementstyle><datagridtextcolumn.editingelementstyle><style targettype="TextBox"><setter property="TextWrapping" value="Wrap"/><setter property="AcceptsReturn" value="true"/></style></datagridtextcolumn.editingelementstyle><datagridcolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="1,1" startpoint="0.5,0" spreadmethod="Pad"><gradientstop color="Lavender" offset="0"/><gradientstop color="BlanchedAlmond" offset="1"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="Width" value="Auto"/><setter property="BorderBrush" value="Yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridcolumn.headerstyle></datagridtextcolumn><datagridtextcolumn header="LastName" fontweight="Bold" foreground="OrangeRed" fontsize="16" fontstyle="Italic">CanUserSort="False" Binding="{Binding Path=LastName}"><datagridtextcolumn.elementstyle><style targettype="TextBlock"><setter property="TextAlignment" value="Center"/></style></datagridtextcolumn.elementstyle><datagridtextcolumn.editingelementstyle><style targettype="TextBox"><setter property="TextWrapping" value="Wrap"/><setter property="AcceptsReturn" value="true"/></style></datagridtextcolumn.editingelementstyle><datagridcolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="1,1" startpoint="0.5,0" spreadmethod="Pad"><gradientstop color="Brown" offset="0"/><gradientstop color="White" offset="1"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="Width" value="Auto"/><setter property="BorderBrush" value="Yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridcolumn.headerstyle></datagridtextcolumn><datagridtemplatecolumn header="DOB" minwidth="100" canusersort="False"><datagridtemplatecolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="1,.1" startpoint="0.5,1" spreadmethod="Pad"><gradientstop color="Orange" offset="1"/><gradientstop color="Thistle" offset="0"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="Width" value="Auto"/><setter property="BorderBrush" value="yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridtemplatecolumn.headerstyle><datagridtemplatecolumn.celleditingtemplate><datatemplate><datepicker selecteddate="{Binding DOB}" selecteddateformat="Short" calendarstyle="DynamicResource CalenderControlTemplate"/></datatemplate></datagridtemplatecolumn.celleditingtemplate><datagridtemplatecolumn.celltemplate><datatemplate><textblock text="{Binding DOB, StringFormat=d}" foreground="DarkTurquoise" horizontalalignment="Center"/></datatemplate></datagridtemplatecolumn.celltemplate></datagridtemplatecolumn><datagridcheckboxcolumn header="Pass/Fail" binding="{Binding Pass}"><datagridcolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="1,1" startpoint="0.5,0" spreadmethod="Pad"><gradientstop color="#FFC2D6F6" offset="0"/><gradientstop color="White" offset="1"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="Width" value="Auto"/><setter property="BorderBrush" value="Yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridcolumn.headerstyle></datagridcheckboxcolumn><datagridtextcolumn header="Nationality" fontweight="Bold" foreground="SandyBrown">FontSize="16" FontStyle="Italic" CanUserSort="False"Binding="{Binding Path= Nationality}" Width="Auto"><datagridtextcolumn.elementstyle><style targettype="TextBlock"><setter property="TextAlignment" value="Center"/></style></datagridtextcolumn.elementstyle><datagridtextcolumn.editingelementstyle><style targettype="TextBox"><setter property="TextWrapping" value="Wrap"/><setter property="AcceptsReturn" value="true"/></style></datagridtextcolumn.editingelementstyle><datagridcolumn.headerstyle><style targettype="{x:Type DataGridColumnHeader}"><setter property="Background"><setter.value><lineargradientbrush endpoint="0.5,1" startpoint="0.5,0" spreadmethod="Pad"><gradientstop color="Blue" offset="0"/><gradientstop color="White" offset="1"/></lineargradientbrush></setter.value></setter><setter property="Foreground" value="Black"/><setter property="FontSize" value="20"/><setter property="HorizontalContentAlignment" value="Center"/><setter property="Width" value="Auto"/><setter property="BorderBrush" value="Yellow"/><setter property="BorderThickness" value="2,2,2,2"/></style></datagridcolumn.headerstyle></datagridtextcolumn></datagrid.columns></datagrid>


I think the problem lies in your xaml with the namespace declaration of:
dg:DataGridTextColumn

what is
dg:
defined as in you xaml.

Try excluding it and see now if you don''t get the problem. because it seems to work fine for me.

问候

本文地址:https://www.itbaoku.cn/post/1395208.html