Style.Triggers与WPF中的属性模板 [英] Style.Triggers with property Template in WPF
问题描述
我希望 设置样式 for 我的DataGrid,但我不知道其中 问题
backgroud属性不工作 其值 的模板属性。
我的代码:
< < span class =code-leadattribute> Window.Resources >
< 样式 x:Key = DataGridStyle1 TargetType = {x:Type DataGrid} >
< Setter 属性 = CellStyle 值 = {DynamicResource GridStyle1} / >
< / Style >
< 样式 x:Key = GridStyle1 TargetType = DataGridCell >
< Style.Triggers >
< 触发器 属性 = IsSelected 值 = 真 > ;
< Setter 属性 = 背景 值 = SeaGreen / >
< / Trigger >
< span class =code-keyword>< / Style.Triggers >
< Style.Setters >
< ; Setter Property < span class =code-keyword> = 模板 >
< Setter.Value >
< ControlTemplate TargetType = < span class =code-keyword> {x:Type DataGridCell} >
< 边框 名称 = DataGridCellBorder >
< ContentControl 内容 = {TemplateBinding Content} >
< ContentControl.ContentTemplate >
< DataTemplate>
< TextBlock Background =Transparentquot; TextWrapping =WrapWithOverflowTextTrimming =CharacterEllipsisHeight =autoWidth =autoText ={Binding Text}/>
< / DataTemplate>
< /ContentControl.ContentTemplate>
< / ContentControl>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< pre lang =xml> < / Window.Resources >
< DataGrid 样式 = {DynamicResource DataGridStyle1} AutoGenerateColumns = True SelectionChanged = grid_Pr_SelectionChanged / >
请帮帮我。
< /Style.Setters>
< / Style> ;
我不能告诉你它的工作原理,但我可以帮你修复错误。从本质上讲,你的模板会覆盖你的单元格样式。
我敢打赌,如果你删除整个模板部分它会起作用。但是,假设您需要该行为,则必须在控件模板中添加它。绑定有点复杂。
< DataTemplate>
< TextBlock TextWrapping =WrapWithOverflow
TextTrimming =CharacterEllipsisHeight =AutoWidth =Auto
Text ={Binding Text}>
< TextBlock.Style>
< Style TargetType ={x:Type TextBlock}>
< Style.Triggers>
< DataTrigger Binding ={绑定IsSelected,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type DataGridCell}}}Value =True>
< Setter Property =BackgroundValue =SeaGreen/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /TextBlock.Style>
< / TextBlock>
< / DataTemplate>
P.S.您应该使用StaticResource而不是DynamicResource。你在这里不需要它,因此你会产生更多的开销。
I want to set a style for my DataGrid, but I do not know where is the problem
the backgroud property does not work with its value in the presence of the Template property.
my code:
<Window.Resources>
<Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
<Setter Property="CellStyle" Value="{DynamicResource GridStyle1}"/>
</Style>
<Style x:Key="GridStyle1" TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="SeaGreen"/>
</Trigger>
</Style.Triggers>
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Name="DataGridCellBorder">
<ContentControl Content="{TemplateBinding Content}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Background="Transparentquot; TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="auto" Width="auto" Text="{Binding Text}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Window.Resources>
<DataGrid Style="{DynamicResource DataGridStyle1}" AutoGenerateColumns="True" SelectionChanged="grid_Pr_SelectionChanged"/>
Help me please.
</Style.Setters>
</Style>
I cannot tell you the reasons it works the way it does, but I can help you fix what is wrong. Essentially, your template is overriding your cell style.
I bet if you remove the whole template section it'll work. But, assuming you want the behavior, you'll have to add it within the control template. The binding is a little more complicated.
<DataTemplate> <TextBlock TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="Auto" Width="Auto" Text="{Binding Text}"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridCell}}}" Value="True"> <Setter Property="Background" Value="SeaGreen" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </DataTemplate>
P.S. You should be using StaticResource not DynamicResource. You don't need it here, and you're incurring a little more overhead because of it.
这篇关于Style.Triggers与WPF中的属性模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!