如何在WPF中居中对齐组合框所选项目 [英] How to center align combobox selected item in WPF

查看:924
本文介绍了如何在WPF中居中对齐组合框所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了实现这一目的,我制作了一个默认组合框的完整副本,并修改了内容呈现器的切换按钮水平对齐到中心



是否有任何简单的方法来实现这比编辑整个模板

解决方案

在ComboBox的ControlTemplate中的ContentPresenter中分配Horizo​​ntalAlignment =Center

 < ControlTemplate x:Key =ComboBoxControlTemplate1TargetType ={x:Type ComboBox}> 
< Grid x:Name =MainGridSnapsToDevicePixels =True>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition MinWidth ={DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}Width =0/>
< /Grid.ColumnDefinitions>
< Popup ...... />
< ToggleButton ..... />
< ContentPresenter ContentTemplate ={TemplateBinding SelectionBoxItemTemplate}Content ={TemplateBinding SelectionBoxItem}ContentStringFormat ={TemplateBinding SelectionBoxItemStringFormat}Horizo​​ntalAlignment =CenterIsHitTestVisible =FalseMargin ={TemplateBinding Padding}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}VerticalAlignment ={TemplateBinding VerticalContentAlignment}/>
< / Grid>
< / ControlTemplate>


I want combobox selected item alone center aligned while the drop down contents to be left aligned.

To achieve this I made an entire copy of the default combobox and modified the Content Presenter of Toggle button horizontal alignment to Center

Is there any simple approach to achieve this than to edit the entire template

解决方案

Assigning the HorizontalAlignment = "Center" in the ContentPresenter in ControlTemplate of the ComboBox

<ControlTemplate x:Key="ComboBoxControlTemplate1" TargetType="{x:Type ComboBox}">
        <Grid x:Name="MainGrid" SnapsToDevicePixels="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
            </Grid.ColumnDefinitions>                
           <Popup ...... />
           <ToggleButton ...../>
           <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="Center" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>
</ControlTemplate>

这篇关于如何在WPF中居中对齐组合框所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆