通过触发器更改父属性

时间:2015-10-01 14:38:03

标签: wpf xaml triggers controltemplate

我有一个ComboBox自定义设计的以下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中,我为IsFocussed属性添加了一个触发器,其值为true,并且想要更改边框颜色和厚度(以及其他内容)

我也尝试过ComboBox风格的这个触发器,但我似乎无法让它完全起作用,而且真的碰到了问题可能是什么问题,任何人都可以对此有所了解吗?< / p>

2 个答案:

答案 0 :(得分:0)

组合框切换按钮永远不会获得焦点,因此将其用作触发器是行不通的。焦点始终与编辑/静态文本或下拉列表中的项目列表保持一致。

答案 1 :(得分:0)

条件必须看起来像这样

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

在切换按钮内进行模板化时。