WPF 组合框样式未绑定 属性

WPF Combobox Style not binding property

在我的 WPF 项目中,我有这种 ComboBox 样式:

<Style x:Key="ComboBoxTextBoxStyle" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <Border CornerRadius="5,0,0,5"
                            
                            Background="{TemplateBinding Background}"
                                >
                            <ScrollViewer x:Name="PART_ContentHost"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="comboboxitem" TargetType="ComboBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <Border CornerRadius="3" Height="30" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center"/>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource BackgroundColour}"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



    <Style x:Key="combobox" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition MaxWidth="18"/>
                        </Grid.ColumnDefinitions>
                        <TextBox Background="{TemplateBinding Background}" Name="PART_EditableTextBox" IsReadOnly="{TemplateBinding IsReadOnly}" FontSize="{TemplateBinding FontSize}" Style="{StaticResource ComboBoxTextBoxStyle}" Foreground="White" VerticalAlignment="Center" Height="{TemplateBinding Height}"/>
                        <ToggleButton Grid.Column="1" Background="{TemplateBinding Background}" Margin="0" Height="{TemplateBinding Height}" Focusable="False" Foreground="White" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press">
                            <ToggleButton.Template>
                                <ControlTemplate>
                                    <Border BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0 5 5 0">
                                        <Border.Background>
                                            <SolidColorBrush Color="{TemplateBinding Background}"/>
                                        </Border.Background>
                                        <Path Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="#DF464B" />
                                    </Border>
                                </ControlTemplate>
                            </ToggleButton.Template>

                        </ToggleButton>
                        <ContentPresenter Name="ContentSite"
                                      Content="{TemplateBinding SelectionBoxItem}"
                                      ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                      ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Left"
                                      Margin="5,0,0,0"/>
                        <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"
                                    BorderThickness="1"
                                    CornerRadius="0 0 5 5"
                                    Background="{TemplateBinding Background}"
                                    BorderBrush="#DF464B"/>
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained">

                                    </StackPanel>
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

但是在箭头下拉部分(样式为边框)我尝试使用 <SolidColorBrush Color="{TemplateBinding Background}"/> 设置 Background 属性 绑定到模板的 属性 但它不会工作。我猜它与以下事实有关:它试图从实际边界获取 Background,而不是当我像这样调用 Combobox 时:<Combobox Style="combobox" Background="DesiredBackground"/>。我怎样才能让它绑定到正确的背景?

您应该将边框的背景 属性 而不是颜色绑定到 ComboBox 背景 属性。

它也适用于“TemplateBinding”,但是您绑定到 ToggleButton 而不是 ComboBox。但是由于 ToggleButton 是从 ComboBox 获取背景的,所以它无论如何都会工作。

   <Border BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0 5 5 0"
           Background="{Binding Background, RelativeSource={RelativeSource FindAncestor, AncestorType=ComboBox}}">