In the style for my custom combobox I want to change the Background
property of the Border
element in the ToggleButton
's ControlTemplate
when the ComboBox.IsEditable
property is set to true but am running into this error targeting the Border
:
Cannot find the Trigger target. (The target must appear before any Setters, Triggers, or Conditions that use it.)
Things I've tried:
- Targeting the Border by it's name:
TargetName="Border"
- Targeting the Border as a child of the ToggleButton:
TargetName="ToggleButton.Border"
The only information I can find online says:
TargetName operates only within the one ControlTemplate section
But I'm not sure if this is relevant to my situation since the ToggleButtons
's ControlTemplate
is a child of my custom combobox's ControlTemplate
.
Style (irrelevant code removed for brevity)
<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>
Border
property in ToggleButton. Anyways, in this case, you don't needTargetName="ToggleButton.Border"
. – emoachtTargetName
is not given, the target will be local:CustomComboBox itself. And you set a binding between Border.Background of "Border" and local:CustomComboBox.Background. Accordingly, this trigger will change local:CustomComboBox.Background and then the value will be reflected to Border.Background of "Border". That's it. – emoacht