简体   繁体   English

如何在WPF中自定义ComboBox

[英]how to customize ComboBox in WPF

always in a ComboBox a arrow button is show where user click and then a list pop down with value. 总是在ComboBox中,会显示一个箭头按钮,用户单击该按钮,然后弹出一个带有值的列表。 in WPF can we change the arrow button where i will use my own arrow mage.functionality will be the same. 在WPF中,我们可以更改箭头按钮,我将使用自己的箭头mage.functionality将是相同的。 if possible then please show me the xaml for that. 如果可能的话请告诉我xaml。 thanks 谢谢

What i understand is that you want to customize the arrow of the combobox and have your custom arrow image.If that is the case then you can easily do it by modifying the controltemplate of combobox. 我的理解是,您想自定义组合框的箭头并具有自定义的箭头图像。如果是这种情况,则可以通过修改组合框的控件模板轻松地做到这一点。

You can edit the default control template by using Expression Blend or copy the same from here and do your modifications. 您可以使用Expression Blend编辑默认控件模板,也可以从此处复制它并进行修改。

The Arrow is represented in the default template as a path inside the togglebutton controltemplate Named 'Arrow' 箭头在默认模板中表示为名为“箭头”的切换按钮控件模板内部的路径

.You can change it as you wish to get what you are looking for 你可以改变它,因为你希望得到你想要的东西

<Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry>
    <Style x:Key="ComboBoxReadonlyToggleButton" 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>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                        <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                            <Path x:Name="Arrow" Fill="Red" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center" Data="{StaticResource DownArrowGeometry}"/>
                       </Grid>
                    </Microsoft_Windows_Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM