如何定位嵌套 ControlTemplate 中的元素
How to target elements in nested ControlTemplates
在自定义组合框的样式中,我想更改 ToggleButton
的 ControlTemplate
中 Border
元素的 Background
属性 ComboBox.IsEditable
属性 设置为 true 但 运行 进入此针对 Border
:
的错误
Cannot find the Trigger target. (The target must appear before any Setters, Triggers, or Conditions that use it.)
我尝试过的事情:
- 按名称定位边界:
TargetName="Border"
- 将边框定位为 ToggleButton 的子项:
TargetName="ToggleButton.Border"
网上唯一能找到的资料是这样说的:
TargetName operates only within the one ControlTemplate section
但我不确定这是否与我的情况相关,因为 ToggleButtons
的 ControlTemplate
是我的自定义组合框 ControlTemplate
.
的子项
样式(为简洁起见删除了不相关的代码)
<Style x:Key="{x:Type local:CustomComboBox}" TargetType="{x:Type local:CustomComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomComboBox}">
<Grid>
<ToggleButton x:Name="ToggleButton" Focusable="False" ClickMode="Press"
VerticalContentAlignment="Center"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="Border" Grid.ColumnSpan="2"
Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}"/>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ComboBox.IsEditable" Value="True">
<Setter Property="Background" TargetName="ToggleButton.Border" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
不是在嵌套模板上设置值,而是相反,通过 [= 在嵌套模板中绑定父 CustomComboBox
的 IsEditable
属性 17=]。如果 属性 是 True
.
,请使用 DataTrigger
将 Border
的颜色设置为 Green
<Style x:Key="{x:Type local:CustomComboBox}" TargetType="{x:Type local:CustomComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomComboBox}">
<Grid>
<ToggleButton x:Name="ToggleButton" Focusable="False" ClickMode="Press"
VerticalContentAlignment="Center"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="Border" Grid.ColumnSpan="2"
Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsEditable, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}" Value="True">
<Setter TargetName="Border" Property="Background" Value="Green"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在自定义组合框的样式中,我想更改 ToggleButton
的 ControlTemplate
中 Border
元素的 Background
属性 ComboBox.IsEditable
属性 设置为 true 但 运行 进入此针对 Border
:
Cannot find the Trigger target. (The target must appear before any Setters, Triggers, or Conditions that use it.)
我尝试过的事情:
- 按名称定位边界:
TargetName="Border"
- 将边框定位为 ToggleButton 的子项:
TargetName="ToggleButton.Border"
网上唯一能找到的资料是这样说的:
TargetName operates only within the one ControlTemplate section
但我不确定这是否与我的情况相关,因为 ToggleButtons
的 ControlTemplate
是我的自定义组合框 ControlTemplate
.
样式(为简洁起见删除了不相关的代码)
<Style x:Key="{x:Type local:CustomComboBox}" TargetType="{x:Type local:CustomComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomComboBox}">
<Grid>
<ToggleButton x:Name="ToggleButton" Focusable="False" ClickMode="Press"
VerticalContentAlignment="Center"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="Border" Grid.ColumnSpan="2"
Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}"/>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="ComboBox.IsEditable" Value="True">
<Setter Property="Background" TargetName="ToggleButton.Border" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
不是在嵌套模板上设置值,而是相反,通过 [= 在嵌套模板中绑定父 CustomComboBox
的 IsEditable
属性 17=]。如果 属性 是 True
.
DataTrigger
将 Border
的颜色设置为 Green
<Style x:Key="{x:Type local:CustomComboBox}" TargetType="{x:Type local:CustomComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomComboBox}">
<Grid>
<ToggleButton x:Name="ToggleButton" Focusable="False" ClickMode="Press"
VerticalContentAlignment="Center"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="Border" Grid.ColumnSpan="2"
Background="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsEditable, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomComboBox}}" Value="True">
<Setter TargetName="Border" Property="Background" Value="Green"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>