【问题标题】:How set Style of WPF ComboBox如何设置 WPF ComboBox 的样式
【发布时间】:2014-11-11 05:50:07
【问题描述】:

我想修改组合框的默认样式。所以我写了这段代码:

        <!-- ComboBox style -->
        <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton Name="ToggleButton" 
                    Template="{StaticResource ComboBoxToggleButton}" 
                    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="3,3,23,3"
                            <TextBox x:Name="PART_EditableTextBox"
                        Style="{x:Null}"  Template="{StaticResource ComboBoxTextBox}" 
                        Text="{TemplateBinding Text}"
                        Foreground="DarkBlue"
                        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"/>
                                    <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
            </Style.Triggers>
        </Style>

这是我在 XAML 文件中的组合框,但我不知道设置我的样式:

<ComboBox x:Name="nomiGiocatori" /> 

我如何应用这个 ComboBox 的新 Stype?

警戒

【问题讨论】:

    标签: wpf combobox styles


    【解决方案1】:

    你的风格有点不对,应该是

    <Style x:Key="YourButtonStyle" TargetType="{x:Type Button}">
    

    在你的 xaml 代码中

    <ComboBox x:Name="nomiGiocatori" Style="{StaticResource YourButtonStyle}"/> 
    

    如果您将样式定义为

    ,则可以将其应用于所有组合框
    <Style TargetType="{x:Type Button}" BasedOn="Button"/>
    

    那么您不必对您的 xaml 执行任何操作。

    This answer describes a similar issue.

    希望对你有帮助,

    斯蒂安

    【讨论】:

      【解决方案2】:

      感谢您的建议,但我认为您不能将目标类型为 Button 的样式应用于 ComboBox 对象。我已经审查了您的建议,但它在 WFP 实施的任何地方都不起作用。即使您使用 BasedOn="Button" 或 "ComboBox"。编译器生成错误消息 TypeConverter for "Style" 不支持从字符串转换。

      ComboBox 基本上包含三个部分:ComboBox、ComboBoxItems 和 ToggleButton 元素。要覆盖默认设置,您必须首先使用属性类型作为模板定义这三种样式,并将 OverridesDefaultStyle 设置为 true。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        • 2011-08-05
        • 2017-11-29
        • 1970-01-01
        相关资源
        最近更新 更多