本文介绍了如何在WPF中居中对齐组合框所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了实现这一目的,我制作了一个默认组合框的完整副本,并修改了内容呈现器的切换按钮水平对齐到中心
是否有任何简单的方法来实现这比编辑整个模板
解决方案在ComboBox的ControlTemplate中的ContentPresenter中分配HorizontalAlignment =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}HorizontalAlignment =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中居中对齐组合框所选项目
发布评论