如何将样式应用于组合框模板

时间:2019-04-25 18:12:05

标签: wpf xaml

我有一个用户控件,它是多选组合框(即comoboxitems是复选框)。我的应用程序中的每个常规组合框都有一个通用样式。除了我创建的控件外,该样式对于每个组合框都适用。当我注释掉组合框模板时,我可以看到组合框样式。问题在于合并了combobox.template部分,并具有应用程序样式。

enter image description here

   <!--  ComboBox  -->
<Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="{StaticResource Brush.Control.Background}" />
    <!--  <Setter Property="CaretBrush" Value="White"/>  -->
    <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground}" />

    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="AllowDrop" Value="True" />
    <Setter Property="MinWidth" Value="0" />
    <Setter Property="MinHeight" Value="0" />
    <Setter Property="FocusVisualStyle" Value="{StaticResource Controls.FocusVisual}" />
    <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <ScrollViewer x:Name="PART_ContentHost"
                              Background="Transparent"
                              BorderThickness="{TemplateBinding BorderThickness}"
                              Focusable="false"
                              Foreground="{TemplateBinding Foreground}"
                              HorizontalScrollBarVisibility="Hidden"
                              VerticalScrollBarVisibility="Hidden" />

                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Highlighted}" />
                    </Trigger>
                    <Trigger Property="IsReadOnly" Value="True">
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Disabled}" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Disabled}" />
                        <Setter Property="Background" Value="{StaticResource Brush.Control.Background.Disabled}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


   <Style x:Key="Controls.ComboBox" TargetType="{x:Type ComboBox}">
    <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground}" />
    <Setter Property="Background" Value="{StaticResource Brush.Control.Background}" />
    <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
    <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
    <Setter Property="FocusVisualStyle" Value="{StaticResource Controls.FocusVisual}" />
    <Setter Property="FontFamily" Value="MS Sans Serif" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="FontSize" Value="10pt" />
    <Setter Property="IsTabStop" Value="True" />
    <Setter Property="Code:ExtendedProperties.CornerRadius" Value="3" />
    <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">

                <Grid x:Name="grid" SnapsToDevicePixels="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Border x:Name="Bd"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            CornerRadius="3,0,0,3"
                            Padding="1"
                            MinHeight="20">
                        <Border x:Name="SelectedItemBorder" Margin="{TemplateBinding Padding}" />
                    </Border>
                    <ContentPresenter x:Name="contentPresenter" Margin="3"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                      ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      Visibility="Visible">
                    </ContentPresenter>
                    <TextBox x:Name="PART_EditableTextBox"
                             Margin="3"
                             HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                             VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                             FontFamily="{TemplateBinding FontFamily}"
                             FontSize="{TemplateBinding FontSize}"
                             FontWeight="{TemplateBinding FontWeight}"
                             ContextMenu="{TemplateBinding ComboBox.ContextMenu}"
                             IsReadOnly="{Binding IsReadOnly,
                                                  RelativeSource={RelativeSource TemplatedParent}}"
                             Style="{StaticResource ComboBoxEditableTextBox}"
                             Visibility="Collapsed" />
                    <!--<Path x:Name="DiffHighlighter" Visibility="Hidden" Data="M18.137,15.136 L18.137,3 C18.137,1.3431458 16.793854,0 15.137,0 L3,0 z" HorizontalAlignment="Right" Height="8.512" VerticalAlignment="Top" Width="8.513">
                        <Path.Fill>
                            <SolidColorBrush Color="{StaticResource quaBlueLightColor}" PresentationOptions:Freeze="true" />
                        </Path.Fill>
                    </Path>-->
                    <ToggleButton x:Name="transparentToggleButton"
                                  IsChecked="{Binding IsDropDownOpen,
                                                      Mode=TwoWay,
                                                      RelativeSource={RelativeSource TemplatedParent}}"
                                  Style="{StaticResource TransparentComboBoxButtonStyle}" />
                    <ToggleButton x:Name="dropDownToggleButton"
                                  Grid.Column="1"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  IsChecked="{Binding IsDropDownOpen,
                                                      Mode=TwoWay,
                                                      RelativeSource={RelativeSource TemplatedParent}}"
                                  RenderTransformOrigin="0.5,0.5"
                                  Style="{StaticResource Controls.ComboBox.Button}"
                                  Width="{Binding ActualHeight, ElementName=grid}"
                                  Height="{Binding ActualHeight, ElementName=grid}">

                        <ToggleButton.RenderTransform>
                            <TranslateTransform X="-1" />
                            <!--<ScaleTransform CenterX="0.5"
                                            CenterY="0.5"
                                            ScaleX="1.1"
                                            ScaleY="1.1" />-->
                        </ToggleButton.RenderTransform>
                    </ToggleButton>

                    <Popup x:Name="PART_Popup"
                           Grid.ColumnSpan="2"
                           AllowsTransparency="True"
                           Focusable="False"
                           IsOpen="{Binding IsDropDownOpen,
                                            RelativeSource={RelativeSource TemplatedParent}}"
                           Placement="Bottom">
                        <!--PopupAnimation="{StaticResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"-->
                        <Border MinWidth="{Binding ActualWidth,
                                                   ElementName=grid}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                Background="Transparent"
                                Padding="0 2 0 0">
                            <Border x:Name="DropDownBorder" BorderThickness="0">
                                <ScrollViewer x:Name="DropDownScrollViewer"
                                              Code:ExtendedProperties.CornerRadius="{Binding Path=(Code:ExtendedProperties.CornerRadius),
                                                                                          RelativeSource={RelativeSource TemplatedParent}}"
                                              Background="{TemplateBinding Background}"
                                              BorderBrush="{TemplateBinding BorderBrush}"
                                              BorderThickness="{TemplateBinding BorderThickness}">
                                    <Grid Margin="3">
                                        <Canvas Width="0"
                                                Height="0"
                                                HorizontalAlignment="Left"
                                                VerticalAlignment="Top">
                                            <Rectangle x:Name="OpaqueRect"
                                                       Width="{Binding ActualWidth,
                                                                       ElementName=DropDownBorder}"
                                                       Height="{Binding ActualHeight,
                                                                        ElementName=DropDownBorder}"
                                                       Fill="{Binding Background,
                                                                      ElementName=DropDownBorder}" />
                                        </Canvas>
                                        <ItemsPresenter x:Name="ItemsPresenter"
                                                        KeyboardNavigation.DirectionalNavigation="Contained"
                                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                    </Grid>
                                </ScrollViewer>
                            </Border>
                        </Border>
                    </Popup>
                </Grid>

                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelectionBoxHighlighted" Value="True" />
                            <Condition Property="IsDropDownOpen" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Highlighted}" />
                    </MultiTrigger>
                    <Trigger Property="IsSelectionBoxHighlighted" Value="True">
                        <Setter TargetName="SelectedItemBorder" Property="Background" Value="{StaticResource Brush.Control.Background.Highlighted}" />
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.HighlightedContrast}" />
                    </Trigger>
                    <Trigger Property="HasItems" Value="False">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
                    </Trigger>
                    <!--<Trigger Property="IsReadOnly" Value="True">
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Disabled}" />
                        <Setter TargetName="dropDownToggleButton" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="transparentToggleButton" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="SelectedItemBorder" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="Bd" Property="CornerRadius" Value="3" />
                        <Setter Property="IsHitTestVisible" Value="False" />
                        <Setter Property="IsTabStop" Value="False" />
                    </Trigger>-->
                    <Trigger Property="IsEditable" Value="True">
                        <Setter TargetName="SelectedItemBorder" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="contentPresenter" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="transparentToggleButton" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsEditable" Value="False">
                        <Setter TargetName="SelectedItemBorder" Property="Visibility" Value="Visible" />
                        <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible" />
                        <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Collapsed" />
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="True">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
                    </Trigger>
                    <Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="False">
                        <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
                        <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsReadOnly" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border.Highlighted}" />
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Highlighted}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition SourceName="dropDownToggleButton" Property="IsMouseOver" Value="True" />
                            <Condition Property="IsReadOnly" Value="False" />
                            <Condition Property="IsEditable" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border.Highlighted}" />
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Highlighted}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsReadOnly" Value="False" />
                            <Condition Property="IsEditable" Value="False" />
                            <Condition Property="IsSelectionBoxHighlighted" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border.Highlighted}" />
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Highlighted}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsReadOnly" Value="False" />
                            <Condition Property="IsEditable" Value="False" />
                            <Condition Property="IsSelectionBoxHighlighted" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border.Highlighted}" />
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.HighlightedContrast}" />
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{StaticResource Brush.Control.Foreground.Disabled}" />
                        <Setter Property="Background" Value="{StaticResource Brush.Control.Background.Disabled}" />
                        <Setter Property="BorderBrush" Value="{StaticResource Brush.Control.Border.Disabled}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


 <UserControl x:Class="MultiSelectComboBox.MultiSelectComboBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         >
       <ComboBox
        x:Name="MultiSelectCombo"  
        Style="{StaticResource {x:Type ComboBox}}"
        SnapsToDevicePixels="True"
        OverridesDefaultStyle="True"
        ScrollViewer.HorizontalScrollBarVisibility="Auto"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        ScrollViewer.CanContentScroll="True"
        IsSynchronizedWithCurrentItem="True" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Title}"
                          IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                          Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                          Click="CheckBox_Click" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
        <ComboBox.Template >
            <ControlTemplate TargetType="ComboBox" >
                <Grid >
                    <ToggleButton 
                        x:Name="ToggleButton" 
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Grid.Column="2" 
                        IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                        Focusable="false"                           
                        ClickMode="Press" 
                        HorizontalContentAlignment="Left" >
                        <ToggleButton.Template>
                            <ControlTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="18"/>
                                    </Grid.ColumnDefinitions>
                                    <Border
                                        x:Name="Border" 
                                        Grid.ColumnSpan="2"
                                        CornerRadius="2"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}" />
                                    <Border 
                                        x:Name="BorderComp" 
                                        Grid.Column="0"
                                        CornerRadius="2" 
                                        Margin="1" 
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}" >
                                        <TextBlock Text="{Binding Path=Text,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
                                              Background="{TemplateBinding Background}" Padding="3" />
                                    </Border>

                                    <Path 
                                        x:Name="Arrow"
                                        Grid.Column="1"     
                                        Fill="White"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Data="M 0 0 L 4 4 L 8 0 Z"/>
                                </Grid>
                            </ControlTemplate>
                        </ToggleButton.Template>
                    </ToggleButton>
                    <Popup 
                        Name="Popup"
                        Placement="Bottom"                        
                        AllowsTransparency="True" 
                        Focusable="False"  IsOpen="{TemplateBinding IsDropDownOpen}"
                        PopupAnimation="Slide">
                        <Grid 
                                Name="DropDown"
                                SnapsToDevicePixels="True"  
                                MinWidth="{TemplateBinding ActualWidth}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border 
                                x:Name="DropDownBorder" 
                                BorderThickness="1" 
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" DataContext="{Binding}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ComboBox.Template>
    </ComboBox>
</UserControl>

预期结果是组合框应该采用应用程序中的样式,即全黑组合框

0 个答案:

没有答案