WPF/C# - dropbox 样式 - 无法单击 ToggleButton 背景

问题描述 投票:0回答:1

我到处收集代码来制作自定义组合框

但我只能点击箭头来选择项目

我希望能够单击整个 ToggleButton,而不仅仅是箭头

由于某种原因,当我点击红色区域时没有任何反应

甚至点击边框似乎也有效

这是风格:

`

<Style x:Key="ComboBoxTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="{DynamicResource ComponentTextColor}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Border CornerRadius="3,0,0,3"
                        BorderThickness="1"
                        Background="{DynamicResource ComponentBgColor}"
                        BorderBrush="{DynamicResource ComponentBorderColor}">
                        <ScrollViewer x:Name="PART_ContentHost"/>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ComboBoxButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border 
                    Background="{DynamicResource BgComponentColor}" 
                    x:Name="border" 
                    CornerRadius="0,3,3,0" 
                    BorderThickness="0,1,1,1"
                    BorderBrush="{DynamicResource ComponentBorderColor}">
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Foreground" Value="{DynamicResource ComponentTextColor}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled, 
            RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="True">
            <Setter Property="Foreground" Value="{DynamicResource ComponentTextColor}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="{DynamicResource ComponentTextColor}" />
</Style>

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid Height="30">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition MaxWidth="18"/>
                    </Grid.ColumnDefinitions>
                    <TextBox x:Name="PART_EditableTextBox" Height="{TemplateBinding Height}" Padding="5,0,0,0" Style="{StaticResource ComboBoxTextBoxStyle}"/>
                    <ToggleButton Grid.Column="1" ClickMode="Press" Focusable="False" Height="{TemplateBinding Height}" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="0" Style="{StaticResource ComboBoxButtonStyle}" >
                        <Grid>
                            <Path Data="M 0 0 L 4 4 L 8 0 Z" Fill="{DynamicResource ComponentBorderColor}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ToggleButton>
                    <ContentPresenter x:Name="ContentSite" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center"/>
                    <Popup x:Name="Popup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" PopupAnimation="Slide" >
                        <Grid x:Name="DropDown" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}" SnapsToDevicePixels="True" Background="{DynamicResource ComponentBgColor}">
                            <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource ComponentBorderColor}" BorderThickness="1" CornerRadius="3"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" >
                                <StackPanel KeyboardNavigation.DirectionalNavigation="Contained" IsItemsHost="True"/>
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

`

组合框很简单,这里没有花哨的技巧:

           ` <ComboBox Name="scalesList"  Grid.Column="1" SelectionChanged="ScaleWheelScaleChanged" SelectedIndex="0" Margin="0,0,0,0" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding name}" />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>`

我尝试了触发器,但它似乎没有做任何事情

请帮助我,我没有主意,我尝试了很多方法,但没有任何效果

感谢您的帮助

c# wpf combobox click togglebutton
1个回答
0
投票

ComponentBgColor 是正确的颜色资源名称 由于某种原因,使用不正确的名称确实导致了问题

© www.soinside.com 2019 - 2024. All rights reserved.