如何在WPF中居中对齐组合框所选项目

编程入门 行业动态 更新时间:2024-10-25 06:26:29
本文介绍了如何在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中居中对齐组合框所选项目

本文发布于:2023-06-10 02:04:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/606269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   所选   项目   如何在   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!