none
WPF Combobox的下拉箭頭用圖片代替 RRS feed

  • 问题

  • 請問各位,我有一個想法,就是把Combobox的下拉箭頭用一張圖片置換,如下圖

    這樣要怎麼去實現?謝謝各位解答!

    2017年11月20日 6:16

答案

  • Hi,

    首先,我建议你查看Combobox的默认模板(在VS中,选中Combobox元素,右键,编辑模板-->模板副本),Combobox基本上由两部分组成,一个ToggleButton和Popup.

    下面一个修改过后的模板供你参考。你把下面的Path部分换成你的图片即可。另外这个模板中添加了一个TextBox, 可以让用户编辑Combobox。

        <!-- #region comboboxstyle1-->
        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="1"
                            CornerRadius="0"/>
                <Border Grid.Column="0"
                            Margin="1"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="0,0,1,0"
                            CornerRadius="0"/>
                <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                          Fill="White"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#808080" />
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                    <Setter Property="Foreground" Value="#888888"/>
                    <Setter TargetName="Arrow" Property="Fill" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    
        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
            <Border x:Name="PART_ContentHost"
                        Background="{TemplateBinding Background}"
                        Focusable="False"/>
        </ControlTemplate>
    
        <Style x:Key="comboboxstyle1" 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="MinWidth" Value="120"/>
            <Setter Property="MinHeight" Value="20"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <Grid>
                            <ToggleButton Name="ToggleButton"
                                              Grid.Column="2"
                                              ClickMode="Press"
                                              Focusable="false"
                                              IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              Template="{StaticResource ComboBoxToggleButton}"/>
                            <ContentPresenter Name="ContentSite"
                                                  Margin="3,3,23,3"
                                                  HorizontalAlignment="Left"
                                                  VerticalAlignment="Center"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  IsHitTestVisible="False"/>
                            <TextBox x:Name="PART_EditableTextBox"
                                         Margin="3,3,23,3"
                                         HorizontalAlignment="Left"
                                         VerticalAlignment="Center"
                                         Background="#FF3F3F3F"
                                         Focusable="True"
                                         Foreground="Green"
                                         IsReadOnly="{TemplateBinding IsReadOnly}"
                                         Style="{x:Null}"
                                         Template="{StaticResource ComboBoxTextBox}"
                                         Visibility="Hidden"/>
                            <Popup Name="Popup"
                                       AllowsTransparency="True"
                                       Focusable="False"
                                       IsOpen="{TemplateBinding IsDropDownOpen}"
                                       Placement="Bottom"
                                       PopupAnimation="Slide">
    
                                <Grid Name="DropDown"
                                          MinWidth="{TemplateBinding ActualWidth}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                          SnapsToDevicePixels="True">
                                    <Border x:Name="DropDownBorder"
                                                Background="#FF3F3F3F"
                                                BorderBrush="#888888"
                                                BorderThickness="1"/>
                                    <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="95"/>
                            </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="0"/>
                                <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>
    
        <!--  SimpleStyles: ComboBoxItem  -->
        <Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Name="Border"
                                    Padding="2"
                                    SnapsToDevicePixels="true">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#FF4F4F4F"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#888888"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!--#endregion-->

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月20日 7:44
    版主

全部回复

  • Hi,

    首先,我建议你查看Combobox的默认模板(在VS中,选中Combobox元素,右键,编辑模板-->模板副本),Combobox基本上由两部分组成,一个ToggleButton和Popup.

    下面一个修改过后的模板供你参考。你把下面的Path部分换成你的图片即可。另外这个模板中添加了一个TextBox, 可以让用户编辑Combobox。

        <!-- #region comboboxstyle1-->
        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="1"
                            CornerRadius="0"/>
                <Border Grid.Column="0"
                            Margin="1"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="0,0,1,0"
                            CornerRadius="0"/>
                <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                          Fill="White"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#808080" />
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                    <Setter Property="Foreground" Value="#888888"/>
                    <Setter TargetName="Arrow" Property="Fill" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    
        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
            <Border x:Name="PART_ContentHost"
                        Background="{TemplateBinding Background}"
                        Focusable="False"/>
        </ControlTemplate>
    
        <Style x:Key="comboboxstyle1" 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="MinWidth" Value="120"/>
            <Setter Property="MinHeight" Value="20"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <Grid>
                            <ToggleButton Name="ToggleButton"
                                              Grid.Column="2"
                                              ClickMode="Press"
                                              Focusable="false"
                                              IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              Template="{StaticResource ComboBoxToggleButton}"/>
                            <ContentPresenter Name="ContentSite"
                                                  Margin="3,3,23,3"
                                                  HorizontalAlignment="Left"
                                                  VerticalAlignment="Center"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  IsHitTestVisible="False"/>
                            <TextBox x:Name="PART_EditableTextBox"
                                         Margin="3,3,23,3"
                                         HorizontalAlignment="Left"
                                         VerticalAlignment="Center"
                                         Background="#FF3F3F3F"
                                         Focusable="True"
                                         Foreground="Green"
                                         IsReadOnly="{TemplateBinding IsReadOnly}"
                                         Style="{x:Null}"
                                         Template="{StaticResource ComboBoxTextBox}"
                                         Visibility="Hidden"/>
                            <Popup Name="Popup"
                                       AllowsTransparency="True"
                                       Focusable="False"
                                       IsOpen="{TemplateBinding IsDropDownOpen}"
                                       Placement="Bottom"
                                       PopupAnimation="Slide">
    
                                <Grid Name="DropDown"
                                          MinWidth="{TemplateBinding ActualWidth}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                          SnapsToDevicePixels="True">
                                    <Border x:Name="DropDownBorder"
                                                Background="#FF3F3F3F"
                                                BorderBrush="#888888"
                                                BorderThickness="1"/>
                                    <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="95"/>
                            </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="0"/>
                                <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>
    
        <!--  SimpleStyles: ComboBoxItem  -->
        <Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Name="Border"
                                    Padding="2"
                                    SnapsToDevicePixels="true">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#FF4F4F4F"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#888888"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!--#endregion-->

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月20日 7:44
    版主
  • Hi,

    首先,我建议你查看Combobox的默认模板(在VS中,选中Combobox元素,右键,编辑模板-->模板副本),Combobox基本上由两部分组成,一个ToggleButton和Popup.

    下面一个修改过后的模板供你参考。你把下面的Path部分换成你的图片即可。另外这个模板中添加了一个TextBox, 可以让用户编辑Combobox。

        <!-- #region comboboxstyle1-->
        <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="1"
                            CornerRadius="0"/>
                <Border Grid.Column="0"
                            Margin="1"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="0,0,1,0"
                            CornerRadius="0"/>
                <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                          Fill="White"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#808080" />
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                    <Setter Property="Foreground" Value="#888888"/>
                    <Setter TargetName="Arrow" Property="Fill" Value="#888888" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    
        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
            <Border x:Name="PART_ContentHost"
                        Background="{TemplateBinding Background}"
                        Focusable="False"/>
        </ControlTemplate>
    
        <Style x:Key="comboboxstyle1" 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="MinWidth" Value="120"/>
            <Setter Property="MinHeight" Value="20"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBox}">
                        <Grid>
                            <ToggleButton Name="ToggleButton"
                                              Grid.Column="2"
                                              ClickMode="Press"
                                              Focusable="false"
                                              IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              Template="{StaticResource ComboBoxToggleButton}"/>
                            <ContentPresenter Name="ContentSite"
                                                  Margin="3,3,23,3"
                                                  HorizontalAlignment="Left"
                                                  VerticalAlignment="Center"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  IsHitTestVisible="False"/>
                            <TextBox x:Name="PART_EditableTextBox"
                                         Margin="3,3,23,3"
                                         HorizontalAlignment="Left"
                                         VerticalAlignment="Center"
                                         Background="#FF3F3F3F"
                                         Focusable="True"
                                         Foreground="Green"
                                         IsReadOnly="{TemplateBinding IsReadOnly}"
                                         Style="{x:Null}"
                                         Template="{StaticResource ComboBoxTextBox}"
                                         Visibility="Hidden"/>
                            <Popup Name="Popup"
                                       AllowsTransparency="True"
                                       Focusable="False"
                                       IsOpen="{TemplateBinding IsDropDownOpen}"
                                       Placement="Bottom"
                                       PopupAnimation="Slide">
    
                                <Grid Name="DropDown"
                                          MinWidth="{TemplateBinding ActualWidth}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                          SnapsToDevicePixels="True">
                                    <Border x:Name="DropDownBorder"
                                                Background="#FF3F3F3F"
                                                BorderBrush="#888888"
                                                BorderThickness="1"/>
                                    <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="95"/>
                            </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="0"/>
                                <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>
    
        <!--  SimpleStyles: ComboBoxItem  -->
        <Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Name="Border"
                                    Padding="2"
                                    SnapsToDevicePixels="true">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#FF4F4F4F"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#888888"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <!--#endregion-->

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    請問在更改ComboBoxStyle1時,出現下面的錯誤,要怎麼排除呢?
    2017年11月21日 7:29
  • Hi,

    错误显示你不能把样式Style应用到ControlTemplete.

    你的ComboBoxToggleButton 是不是定义错了?你是否定义成Style了?

    Templete属性应该对应模板,而不是样式...

    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">

    ...


    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月21日 10:09
    版主
  • Hi,

    错误显示你不能把样式Style应用到ControlTemplete.

    你的ComboBoxToggleButton 是不是定义错了?你是否定义成Style了?

    Templete属性应该对应模板,而不是样式...

    <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">

    ...


    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    我後來有發現我忘記把,x:Key="ComboBoxToggleButton",這段打上去

    可是打上去之後出現了另一個錯誤,可不可以請問你這是為什麼?謝謝,麻煩你了

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1"
            xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Class="WpfApplication1.MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
    
        <Grid>
    
            <ComboBox x:Name="comboBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="159,149,0,0" Style="{DynamicResource ComboBoxStyle1}">
                <ComboBox.Resources>
                    <Style x:Key="FocusVisual">
                        <Setter Property="Control.Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                                </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>
                    <SolidColorBrush x:Key="ComboBox.Static.Border" Color="#FFACACAC"/>
                    <SolidColorBrush x:Key="ComboBox.Static.Editable.Background" Color="#FFFFFFFF"/>
                    <SolidColorBrush x:Key="ComboBox.Static.Editable.Border" Color="#FFABADB3"/>
                    <SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Background" Color="Transparent"/>
                    <SolidColorBrush x:Key="ComboBox.Static.Editable.Button.Border" Color="Transparent"/>
                    <SolidColorBrush x:Key="ComboBox.MouseOver.Glyph" Color="#FF000000"/>
                    <LinearGradientBrush x:Key="ComboBox.MouseOver.Background" EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFECF4FC" Offset="0.0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1.0"/>
                    </LinearGradientBrush>
                    <SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
                    <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Background" Color="#FFFFFFFF"/>
                    <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Border" Color="#FF7EB4EA"/>
                    <LinearGradientBrush x:Key="ComboBox.MouseOver.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFEBF4FC" Offset="0.0"/>
                        <GradientStop Color="#FFDCECFC" Offset="1.0"/>
                    </LinearGradientBrush>
                    <SolidColorBrush x:Key="ComboBox.MouseOver.Editable.Button.Border" Color="#FF7EB4EA"/>
                    <SolidColorBrush x:Key="ComboBox.Pressed.Glyph" Color="#FF000000"/>
                    <LinearGradientBrush x:Key="ComboBox.Pressed.Background" EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFDAECFC" Offset="0.0"/>
                        <GradientStop Color="#FFC4E0FC" Offset="1.0"/>
                    </LinearGradientBrush>
                    <SolidColorBrush x:Key="ComboBox.Pressed.Border" Color="#FF569DE5"/>
                    <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Background" Color="#FFFFFFFF"/>
                    <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Border" Color="#FF569DE5"/>
                    <LinearGradientBrush x:Key="ComboBox.Pressed.Editable.Button.Background" EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#FFDAEBFC" Offset="0.0"/>
                        <GradientStop Color="#FFC4E0FC" Offset="1.0"/>
                    </LinearGradientBrush>
                    <SolidColorBrush x:Key="ComboBox.Pressed.Editable.Button.Border" Color="#FF569DE5"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Glyph" Color="#FFBFBFBF"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Background" Color="#FFF0F0F0"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Border" Color="#FFD9D9D9"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Background" Color="#FFFFFFFF"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Border" Color="#FFBFBFBF"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Background" Color="Transparent"/>
                    <SolidColorBrush x:Key="ComboBox.Disabled.Editable.Button.Border" Color="Transparent"/>
                    <SolidColorBrush x:Key="ComboBox.Static.Glyph" Color="#FF606060"/>
                    <Style x:Key="ComboBoxToggleButton" 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>
    
                                <!-- #region comboboxstyle1-->
                                <ControlTemplate x:Key="ComboBoxToggleButton"  TargetType="{x:Type ToggleButton}">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition Width="20"/>
                                        </Grid.ColumnDefinitions>
                                        <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="1"
                            CornerRadius="0"/>
                                        <Border Grid.Column="0"
                            Margin="1"
                            Background="#FF3F3F3F"
                            BorderBrush="#FF97A0A5"
                            BorderThickness="0,0,1,0"
                            CornerRadius="0"/>
                                        <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"
                          Fill="White"/>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                                            <Setter TargetName="Border" Property="Background" Value="#808080" />
                                        </Trigger>
                                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                                            <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                                            <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                                            <Setter Property="Foreground" Value="#888888"/>
                                            <Setter TargetName="Arrow" Property="Fill" Value="#888888" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
    
                    <ControlTemplate x:Key="ComboBoxTextBox" TargetType="{x:Type TextBox}">
                        <Border x:Name="PART_ContentHost"
                        Background="{TemplateBinding Background}"
                        Focusable="False"/>
                    </ControlTemplate>
                    
                    <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" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                                <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                                    <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                        <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>
                                </Themes:SystemDropShadowChrome>
                            </Popup>
                            <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
                            <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <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>
                    <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>
                    <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}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                                <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                                    <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                        <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>
                                </Themes:SystemDropShadowChrome>
                            </Popup>
                            <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource 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>
                    <Style x:Key="ComboBoxStyle1" 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="MinWidth" Value="120"/>
                        <Setter Property="MinHeight" Value="20"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ComboBox}">
                                    <Grid>
                                        <ToggleButton Name="ToggleButton"
                                              Grid.Column="2"
                                              ClickMode="Press"
                                              Focusable="false"
                                              IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                              Template="{StaticResource ComboBoxToggleButton}"/>
                                        <ContentPresenter Name="ContentSite"
                                                  Margin="3,3,23,3"
                                                  HorizontalAlignment="Left"
                                                  VerticalAlignment="Center"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  IsHitTestVisible="False"/>
                                        <TextBox x:Name="PART_EditableTextBox"
                                         Margin="3,3,23,3"
                                         HorizontalAlignment="Left"
                                         VerticalAlignment="Center"
                                         Background="#FF3F3F3F"
                                         Focusable="True"
                                         Foreground="Green"
                                         IsReadOnly="{TemplateBinding IsReadOnly}"
                                         Style="{x:Null}"
                                         Template="{StaticResource ComboBoxTextBox}"
                                         Visibility="Hidden"/>
                                        <Popup Name="Popup"
                                       AllowsTransparency="True"
                                       Focusable="False"
                                       IsOpen="{TemplateBinding IsDropDownOpen}"
                                       Placement="Bottom"
                                       PopupAnimation="Slide">
    
                                            <Grid Name="DropDown"
                                          MinWidth="{TemplateBinding ActualWidth}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                          SnapsToDevicePixels="True">
                                                <Border x:Name="DropDownBorder"
                                                Background="#FF3F3F3F"
                                                BorderBrush="#888888"
                                                BorderThickness="1"/>
                                                <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="95"/>
                                        </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="0"/>
                                            <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>
                    <!--  SimpleStyles: ComboBoxItem  -->
                    <Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="SnapsToDevicePixels" Value="true"/>
                        <Setter Property="Foreground" Value="White"/>
                        <Setter Property="OverridesDefaultStyle" Value="true"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                    <Border Name="Border"
                                    Padding="2"
                                    SnapsToDevicePixels="true">
                                        <ContentPresenter/>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsHighlighted" Value="true">
                                            <Setter TargetName="Border" Property="Background" Value="#FF4F4F4F"/>
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="false">
                                            <Setter Property="Foreground" Value="#888888"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ComboBox.Resources>
            </ComboBox>
    
    
        </Grid>
    </Window>
    

    2017年11月21日 10:19
  • Hi,

    根据错误信息,你应该是没有把资源放在资源里面。

    Sincerely,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年11月21日 11:10
    版主