Wpf 通过触发器更改父属性

Wpf 通过触发器更改父属性,wpf,xaml,triggers,controltemplate,Wpf,Xaml,Triggers,Controltemplate,我有以下用于组合框定制设计的XAML <ControlTemplate TargetType="{x:Type ToggleButton}" x:Key="ComboBoxToggleButtonTemplate"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefiniti

我有以下用于组合框定制设计的XAML

<ControlTemplate TargetType="{x:Type ToggleButton}" x:Key="ComboBoxToggleButtonTemplate">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="32" />
        </Grid.ColumnDefinitions>
        <Border BorderBrush="{StaticResource CleoComboBoxBorderBrush}" BorderThickness="1,1,1,1" x:Name="Border" Background="{StaticResource CleoComboBoxBackgroundBrush}" Grid.ColumnSpan="2" />
        <Border x:Name="Border2" Margin="1,1,1,1" BorderBrush="{StaticResource CleoComboBoxBorderBrush}" BorderThickness="0,0,0,0" Background="{StaticResource CleoComboBoxBackgroundBrush}" Grid.Column="0" />
        <Path Margin="0,0,3,0" Data="M0,0 L4,4 8,0z" HorizontalAlignment="Center" Fill="{StaticResource CleoComboBoxToggleButtonBrush}" x:Name="Arrow" VerticalAlignment="Center" Width="8" Grid.Column="1" />
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource CleoComboBoxBorderSelectedBrush}" />
            <Setter Property="BorderThickness" TargetName="Border" Value="2" />
            <Setter Property="Shape.Fill" TargetName="Arrow" Value="{StaticResource CleoComboBoxBorderSelectedBrush}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource CleoComboBoxBorderHoverBrush}" />
            <Setter Property="BorderThickness" TargetName="Border" Value="2" />
            <Setter Property="BorderBrush" TargetName="Border2" Value="{StaticResource CleoComboBoxBorderHoverBrush}" />
            <Setter Property="BorderThickness" TargetName="Border2" Value="1,1,0,1" />
            <Setter Property="Shape.Fill" TargetName="Arrow" Value="{StaticResource CleoComboBoxBorderHoverBrush}" />
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource CleoComboBoxBorderSelectedBrush}" />
            <Setter Property="BorderThickness" TargetName="Border" Value="2" />
            <Setter Property="BorderBrush" TargetName="Border2" Value="{StaticResource CleoComboBoxBorderSelectedBrush}" />
            <Setter Property="BorderThickness" TargetName="Border2" Value="1,1,0,1" />
            <Setter Property="Shape.Fill" TargetName="Arrow" Value="{StaticResource CleoComboBoxBorderSelectedBrush}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" TargetName="Border" Value="#FFEEEEEE" />
            <Setter Property="BorderBrush" TargetName="Border" Value="#FFAAAAAA" />
            <Setter Property="Background" TargetName="Border2" Value="#FFEEEEEE" />
            <Setter Property="BorderBrush" TargetName="Border2" Value="#FFAAAAAA" />
            <Setter Property="Foreground" Value="#FF888888" />
            <Setter Property="Shape.Fill" TargetName="Arrow" Value="#66ADADAD" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<ControlTemplate TargetType="{x:Type TextBox}" x:Key="ComboBoxTextBoxTemplate">
    <Border x:Name="PART_ContentHost" Background="{StaticResource CleoComboBoxBackgroundBrush}" Focusable="False" />
</ControlTemplate>

<Style TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Foreground" Value="{StaticResource CleoComboBoxTextBrush}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <Border x:Name="Border" SnapsToDevicePixels="True" Padding="4,4,4,4" BorderBrush="Transparent" BorderThickness="1,1,1,1">
                    <ContentControl ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="True">
                        <Setter Property="Background" TargetName="Border" Value="{StaticResource CleoComboBoxHighlightedItemBackgroundBrush}"/>
                        <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource CleoComboBoxHighlightedItemBorderBrush}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="#FF888888"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
    <Setter Property="TextElement.Foreground" Value="{StaticResource CleoComboBoxTextBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid x:Name="Grid">
                    <ToggleButton ClickMode="Press" x:Name="ToggleButton" IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                  Focusable="False" Grid.Column="2" Template="{StaticResource ComboBoxToggleButtonTemplate}" />
                    <ContentPresenter Margin="3,3,23,3" HorizontalAlignment="Left" x:Name="ContentSite" VerticalAlignment="Center" 
                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" IsHitTestVisible="False" />
                    <TextBox Margin="3,3,23,3" Visibility="Hidden" HorizontalAlignment="Left" x:Name="PART_EditableTextBox" Background="Transparent"
                             VerticalAlignment="Center" Style="{x:Null}" IsReadOnly="False" Focusable="True" xml:space="preserve" Template="{StaticResource ComboBoxTextBoxTemplate}" />
                    <Popup Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" IsOpen="{TemplateBinding IsDropDownOpen}" PopupAnimation="Fade">
                        <Grid MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}" x:Name="DropDown" SnapsToDevicePixels="True">
                            <Border BorderBrush="{StaticResource CleoComboBoxBorderBrush}" BorderThickness="1,1,1,1" x:Name="DropDownBorder" Background="{StaticResource CleoComboBoxBackgroundBrush}" />
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="False">
                        <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="#FF888888" />
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="True">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
                    </Trigger>
                    <Trigger Property="Window.AllowsTransparency" SourceName="Popup" Value="True">
                        <Setter Property="Margin" TargetName="DropDownBorder" Value="0,2,0,0" />
                    </Trigger>
                    <Trigger Property="IsEditable" Value="True">
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
                        <Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible" />
                        <Setter Property="Visibility" TargetName="ContentSite" Value="Hidden" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ComboBoxToggleButtonTemplate
中,我为
IsFocused
属性添加了一个触发器,其中的值为true,并希望更改边框颜色和厚度(以及其他内容)


我也尝试过组合框风格的触发器,但我似乎根本无法让它工作,而且对于可能出现的问题,我真的遇到了麻烦,有人能解释一下吗?

组合框切换按钮永远无法获得焦点,因此将其用作触发器是行不通的。焦点始终停留在编辑/静态文本或下拉列表中的项目列表上。

条件必须如下所示

<Condition Binding="{Binding IsFocussed, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ComboBox}}}" Value="true"/>


当从切换按钮内进行模板设置时。

ok,那么如何实现
IsFocussed
结果给出上述XAML,我想更改边框,就像我使用
IsMouseOver
所做的那样,只对
IsFocussed
进行更改,如何确定组合框的
ContentPresenter
TextBox
是否具有焦点?还是我找错了树了?您必须更改为DataTrigger,并使用RelativeSource绑定来追踪ComboBox控件,然后我可能会使用IsKeyboardFocusWithin属性。比如{Binding RelativeSource={RelativeSource Mode=FindAncestor,antestortype=ComboBox}Path=IsKey}。绑定语法仅适用于前面提到的DataTrigger:)。