根据项目的样式在所选项目上的WPF特定样式

时间:2014-09-25 18:34:33

标签: wpf

我有一个自定义组合框,我在第一个项目上应用特定样式。我希望在选择此项目时,组合框中显示的文本样式是相同的。目前,所有选定的项目都以相同的样式显示,我不知道如何制作ContentPresenter" get"该项目中指定的样式。这是代码:

<ComboBox HorizontalAlignment="Center" Margin="0,106,0,0" VerticalAlignment="Top" Width="200">
    <ComboBoxItem Style="{StaticResource mySpecialStyle}">Select an option...</ComboBoxItem>
    <ComboBoxItem>ComboBox Item #1</ComboBoxItem>
    <ComboBoxItem>ComboBox Item #2</ComboBoxItem>
    <ComboBoxItem>ComboBox Item #3</ComboBoxItem>
</ComboBox>

事实上,&#34; mySpecialStyle&#34;只更改颜色和fontstyle。但是当选择第一个项目时,它看起来就像任何其他所选项目一样。如何在Contentpresenter上设置它?

以下是自定义ComBoBox的完整代码:

<Window.Resources>
    <Style x:Key="mySpecialStyle" TargetType="{x:Type ComboBoxItem}">
        <Setter Property="TextElement.FontStyle" Value="Italic"/>
    </Style>

    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="20"/>
            </Grid.ColumnDefinitions>
            <Border
              x:Name="Border" 
              Grid.ColumnSpan="3"
              CornerRadius="3"
              Background="#FFFAFAFA"
              BorderBrush="#FF999999"
              BorderThickness="1" />
            <Border
              x:Name="Background"
              Grid.Column="0"
              CornerRadius="3,0,0,3" 
              Margin="1"
              Background="#FFFAFAFA" 
              BorderBrush="#FF999999"
              BorderThickness="0" />
            <Path 
              x:Name="ArrowDw"
              Grid.Column="1"     
              Fill="#FF404040"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Data="M 0 0 L 4 4 L 8 0 Z"/>
            <Path 
              x:Name="ArrowUp"
              Grid.Column="1"     
              Fill="Transparent"
              HorizontalAlignment="Center"
              VerticalAlignment="Center"
              Data="M 0 2 L 4 -2 L 8 2 Z"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                <Setter TargetName="ArrowDw" Property="Fill" Value="#FF000000" />
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="true">
                <Setter TargetName="Background" Property="Background" Value="#FFFFFFFF" />
                <Setter TargetName="Border" Property="BorderBrush" Value="#FF000000" />
                <Setter TargetName="Border" Property="Background" Value="#FFFFFFFF" />
                <Setter TargetName="ArrowDw" Property="Fill" Value="Transparent" />
                <Setter TargetName="ArrowUp" Property="Fill" Value="#FF404040" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ToggleButton.IsChecked" Value="True" />
                    <Condition Property="ToggleButton.IsMouseOver" Value="True" />
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter TargetName="ArrowUp" Property="Fill" Value="#FF000000" />
                </MultiTrigger.Setters>
            </MultiTrigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Border" Property="Background" Value="#FFFFFF" />
                <Setter TargetName="Border" Property="BorderBrush" Value="#CCCCCC" />
                <Setter Property="Foreground" Value="#888888"/>
                <Setter TargetName="ArrowDw" Property="Fill" Value="#999999" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

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

    <Style x:Key="{x:Type ComboBox}" 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="MinHeight" Value="27"/>
        <Setter Property="ToolTip" Value="{Binding Path=SelectionBoxItem, RelativeSource={RelativeSource Self}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <ToggleButton 
                          Name="ToggleButton" 
                          Template="{StaticResource ComboBoxToggleButton}" 
                          Grid.Column="2" 
                          Focusable="false"
                          IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                          ClickMode="Press">
                        </ToggleButton>

                        <ContentPresenter
                          Name="ContentSite"
                          IsHitTestVisible="False" 
                          Content="{TemplateBinding SelectionBoxItem}"
                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

                          ????

                          Margin="8,3,28,3"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Left" />
                        <TextBox x:Name="PART_EditableTextBox"
                          Style="{x:Null}" 
                          Template="{StaticResource ComboBoxTextBox}" 
                          HorizontalAlignment="Left" 
                          VerticalAlignment="Center" 
                          Margin="8,3,28,3"
                          Focusable="True" 
                          Background="Transparent"
                          Visibility="Hidden"
                          IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <Popup 
                          Name="Popup"
                          Placement="Bottom"
                          IsOpen="{TemplateBinding IsDropDownOpen}"
                          AllowsTransparency="True" 
                          Focusable="False"
                          PopupAnimation="Slide">
                            <Grid 
                              Name="DropDown"
                              SnapsToDevicePixels="True"                
                              MinWidth="{TemplateBinding ActualWidth}"
                              MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border 
                                  x:Name="DropDownBorder"
                                  Background="#FFFFFF"
                                  BorderThickness="1"
                                  BorderBrush="#FF999999"/>
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="27"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#888888"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </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>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
        </Style.Triggers>
    </Style>
</Window.Resources>

占位符&#34; ???&#34;我需要根据ComboBoxitem中定义的样式设置样式(在这种情况下,&#34; mySpecialStyle&#34;)。

提前致谢!

1 个答案:

答案 0 :(得分:0)

这里要解决的逻辑当然会基于像Trigger这样的东西。但是,我们无法通过Style设置Setter。所以我想到了这个黑客(但足够安全)。首先,您需要将Style的{​​{1}}绑定到其ContentPresenter属性(默认情况下为2路绑定)。然后,您只需将Tag更改为Tag,其中{StaticResource}与您在树上向上定义的资源相同。该资源当然应该ResourceKey TargetType

ContentPresenter