wpf user control, не работает триггер
Я добавил к контролу свойство IsDropClose. При закрытии списка оно принимает значение true. Не могу разобраться, как указать триггер в другом стиле ComboBoxToggleButton. В этом стиле мне нужно при срабатывание триггера изменить цвет у arrow. Пробовал так, но это не работает:
<Trigger Property="local:CustomComboBox.IsDropClose" Value="True">
<Setter TargetName="arrow" Property="Background" Value="Blue"/>
</Trigger>
Код UserControl:
<UserControl x:Class="DELETEEE.CustomComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DELETEEE"
mc:Ignorable="d" xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
d:DesignHeight="450" d:DesignWidth="800" Name="CustComboBox">
<UserControl.Resources>
<local:CustomComboBox x:Key="customComboBox"/>
<!--Цвет фона Combobox-->
<SolidColorBrush x:Key="ColorBackground_ComboBox">#FFEF89E7</SolidColorBrush>
<!--Цвет фона при наведение мыши на ComboBox-->
<SolidColorBrush x:Key="ColorBackground_MouseOver_ComboBox">Yellow</SolidColorBrush>
<!--Цвет BorderBrush у Combobox-->
<SolidColorBrush x:Key="ColorBorderBrush_ComboBox">#FF291E28</SolidColorBrush>
<!--Цвет при нажатии\подстветки BorderBrush Combobox-->
<SolidColorBrush x:Key="ColorBorderBrush_MouseOver_ComboBox">Blue</SolidColorBrush>
<!--Цвет фона стрелочки у Combobox-->
<SolidColorBrush x:Key="ColorBackground_ComboBoxArrow">#BBBBBB</SolidColorBrush>
<!--Цвет фона при наведение мыши на стрелочку ComboBox-->
<SolidColorBrush x:Key="ColorBackground_MouseOver_ComboBoxArrow">Blue</SolidColorBrush>
<!--Цвет тени у выпадающей панели ComboBox-->
<Color x:Key="ColorShadow_ComboBox">Black</Color>
<!--Цвет при выборе Items ComboBox-->
<SolidColorBrush x:Key="ColorBackground_IsSelected_ComboBox">Green</SolidColorBrush>
<!--Цвет при наведение на Items ComboBox-->
<SolidColorBrush x:Key="ColorBackground_MouseOver_ComboBoxItem" Color="Gray"/>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="TextBox.Static.Background" Color="#FFFFFFFF"/>
<Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="MinWidth" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="ComboBox.Static.Background" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF0F0F0" Offset="0.0"/>
<GradientStop Color="#FFE5E5E5" Offset="1.0"/>
</LinearGradientBrush>
<!--Кнопка ComboBox-->
<Style x:Key="ComboBoxToggleButton" x:Name="Test" TargetType="{x:Type ToggleButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="ClickMode" Value="Press"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border x:Name="templateRoot" CornerRadius="20" Background="{DynamicResource ColorBackground_ComboBox}" BorderBrush="{DynamicResource ColorBorderBrush_ComboBox}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<!--Отрисовка стрелочки-->
<Border x:Name="splitBorder" Margin="0 0 10 0" SnapsToDevicePixels="true" VerticalAlignment="Center" HorizontalAlignment="Right" Height="30">
<Viewbox x:Name="viewbox" Width="34" Height="34">
<Path x:Name="arrow" Data="M0.21967 1.28033C-0.0732236 0.987436 -0.073223 0.512563 0.21967 0.21967C0.512563 -0.0732233 0.987437 -0.0732233 1.28033 0.21967L6.21967 5.15901L11.159 0.21967C11.4519 -0.073223 11.9268 -0.0732232 12.2197 0.21967C12.5126 0.512563 12.5126 0.987437 12.2197 1.28033L7.11632 6.38368C6.62111 6.87889 5.81823 6.87889 5.32302 6.38368L0.21967 1.28033Z"
RenderTransformOrigin="0.5,0.5" Fill="{DynamicResource ColorBackground_ComboBoxArrow}">
<Path.RenderTransform>
<RotateTransform></RotateTransform>
</Path.RenderTransform>
</Path>
</Viewbox>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="local:CustomComboBox.IsDropClose" Value="True">
<Setter TargetName="templateRoot" Property="Background" Value="Blue"/>
</Trigger>
<!--Тригер при наведение мыши на шапку ComboBox--><!--
<Trigger Property="IsMouseOver" Value="true">
--><!--Цвет при наведение на стрелочку--><!--
<Setter Property="Fill" TargetName="arrow" Value="{DynamicResource ColorBackground_MouseOver_ComboBoxArrow}"/>
--><!--Цвет при наведение на ComboBox--><!--
<Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource ColorBackground_MouseOver_ComboBox}"/>
<Setter Property="BorderBrush" TargetName="templateRoot" Value="{DynamicResource ColorBorderBrush_MouseOver_ComboBox}"/>
</Trigger>
--><!--Тригер нажатия на стрелочку--><!--
<Trigger Property="IsChecked" Value="true">
<Setter Property="BorderBrush" TargetName="templateRoot" Value="{DynamicResource ColorBorderBrush_MouseOver_ComboBox}"/>
<Setter Property="Fill" TargetName="arrow" Value="{DynamicResource ColorBackground_MouseOver_ComboBoxArrow}"/>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="arrow">
--><!--Отражение по вертикали <DoubleAnimation Duration="00:00:00.100" To="-1" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)"/>--><!--
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
From="0" To="180" Duration="0:0:00.500"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="arrow">
--><!--Отражение по вертикали <DoubleAnimation Duration="00:00:00.100" To="1" Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)"/>--><!--
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
From="180" To="360" Duration="0:0:00.500"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
<Grid x:Name="templateRoot" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
<theme:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="dropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1">
<ScrollViewer x:Name="DropDownScrollViewer">
<Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
<Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ScrollViewer>
</Border>
</theme:SystemDropShadowChrome>
</Popup>
<ToggleButton x:Name="toggleButton" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{DynamicResource ComboBoxToggleButton}"/>
<Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}">
<TextBox x:Name="PART_EditableTextBox" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
<Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/>
<Setter Property="Color" TargetName="shadow" Value="#71000000"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true"/>
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</MultiTrigger>
<Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
<Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
<Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--Панель с контентом(Items) у Combobox-->
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
<Grid x:Name="templateRoot" SnapsToDevicePixels="true">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}">
<theme:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MinWidth="{Binding ActualWidth, ElementName=templateRoot}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border CornerRadius="20" x:Name="dropDownBorder" Margin="2 10 2 2" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" >
<ScrollViewer x:Name="DropDownScrollViewer">
<ItemsPresenter x:Name="ItemsPresenter" Margin="0 10 0 0" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
<Border.Effect>
<DropShadowEffect Color="{DynamicResource ColorShadow_ComboBox}" Opacity="0.14" ShadowDepth="0" BlurRadius="5"></DropShadowEffect>
</Border.Effect>
</Border>
</theme:SystemDropShadowChrome>
</Popup>
<ToggleButton x:Name="toggleButton" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
<ContentPresenter x:Name="contentPresenter" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true"/>
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Padding" Value="6,3,5,3"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
<Style.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="FocusVisual1">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" StrokeDashArray="1 2" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" SnapsToDevicePixels="true" StrokeThickness="1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Список(Items) ComboBox-->
<Style x:Key="ComboBoxItemContainerStyle1" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual1}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Bd" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"/>
<Condition Property="IsMouseOver" Value="False"/>
<Condition Property="IsKeyboardFocused" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ColorBackground_IsSelected_ComboBox}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"/>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ColorBackground_IsSelected_ComboBox}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True"/>
<Condition Property="IsMouseOver" Value="False"/>
<Condition Property="IsKeyboardFocused" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ColorBackground_IsSelected_ComboBox}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False"/>
<Condition Property="IsMouseOver" Value="False"/>
<Condition Property="IsKeyboardFocused" Value="True"/>
</MultiTrigger.Conditions>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="False"/>
<Condition Property="IsMouseOver" Value="True"/>
<Condition Property="IsKeyboardFocused" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource ColorBackground_MouseOver_ComboBoxItem}"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<ComboBox Visibility="Visible" ItemContainerStyle="{DynamicResource ComboBoxItemContainerStyle1}" Style="{DynamicResource ComboBoxStyle1}" VerticalContentAlignment="Center" SelectedIndex="0" Margin="0,0,65,145" DropDownClosed="ComboBox_DropDownClosed">
<TextBlock>Text Text Text Text Text Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
<TextBlock>Text</TextBlock>
</ComboBox>
</Grid>
</UserControl>
Код позади:
public partial class CustomComboBox : UserControl
{
public CustomComboBox()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty IsDropCloseProperty =
DependencyProperty.Register("IsDropClose", typeof(bool), typeof(CustomComboBox));
public bool IsDropClose
{
get { return (bool)GetValue(IsDropCloseProperty); }
set { SetValue(IsDropCloseProperty, value); }
}
private void ComboBox_DropDownClosed(object sender, EventArgs e)
{
IsDropClose = true;
}
}
Источник: Stack Overflow на русском