如何在ListBoxItem中居中WPF CheckBox

如何在ListBoxItem中居中WPF CheckBox,第1张

概述我有一个使用ItemContainerStyle的ListBox.我已经尝试了我能想到的一切,让CheckBox控件垂直和水平居中.有任何想法吗? <ListBox IsSynchronizedWithCurrentItem="True" Height="Auto" Width="Auto" DockPanel.Dock="Top" ItemContainerStyle="{Stat 我有一个使用ItemContainerStyle的ListBox.我已经尝试了我能想到的一切,让CheckBox控件垂直和水平居中.有任何想法吗?

<ListBox  IsSynchronizeDWithCurrentItem="True"   Height="auto" WIDth="auto" DockPanel.Dock="top"  ItemContainerStyle="{StaticResource lbcStyle}" /><Style targettype="ListBoxItem" x:Key="lbcStyle">    <Style.Triggers>        <Trigger Property="IsSelected" Value="True">          <Setter Property="ContentTemplate" Value="{StaticResource editable}"/>        </Trigger>    </Style.Triggers>    <Setter Property="ContentTemplate" Value="{StaticResource nonEditable}"/>    <Setter Property="HorizontalContentAlignment" Value="Center"/> '//i have trIEd stretch here also    <Setter Property="VerticalContentAlignment" Value="Stretch"/></Style>

CheckBoxes得到这种风格:

<Style x:Key="editorCheckBox" targettype="{x:Type CheckBox}">    <Setter Property="MinWIDth" Value="67" />    <Setter Property="Height" Value="25" />    <Setter Property="margin" Value="5,5,0" />    <Setter Property="VerticalAlignment" Value="Center" />    <Setter Property="HorizontalAlignment" Value="Center" /></Style>

以下是可编辑/不可编辑的:

<DataTemplate x:Key="editable">              <border x:name="brdEditable" WIDth="auto" HorizontalAlignment="Stretch">                <DockPanel x:name="dpdEditable" LastChildFill="True" WIDth="auto" Height="auto">                  <GrID x:name="GrdEditable" WIDth="auto" Height="auto">                    <GrID.ColumnDeFinitions>                      <ColumnDeFinition WIDth="25" />                      <ColumnDeFinition WIDth="25" />                      <ColumnDeFinition WIDth="100" />                      <ColumnDeFinition WIDth="100" />                      <ColumnDeFinition WIDth="80" />                      <ColumnDeFinition WIDth="110" />                      <ColumnDeFinition WIDth="110" />                      <ColumnDeFinition WIDth="60" />                      <ColumnDeFinition WIDth="90" />                    </GrID.ColumnDeFinitions>                    <GrID.RowDeFinitions>                      <RowDeFinition></RowDeFinition>                      <RowDeFinition></RowDeFinition>                    </GrID.RowDeFinitions>                    '...                    <CheckBox x:name="chkActive" GrID.Column="7" Height="25" Style="{StaticResource editorCheckBox}" tooltip="Is Construction Active?" IsEnabled="true" ValIDation.ErrorTemplate="{StaticResource valIDationTemplate}">                      <CheckBox.IsChecked>                        <Binding Path="Active">                          <Binding.ValIDationRules>                            <DataErrorValIDationRule></DataErrorValIDationRule>                            <ExceptionValIDationRule></ExceptionValIDationRule>                          </Binding.ValIDationRules>                        </Binding>                      </CheckBox.IsChecked>                    </CheckBox>                    '...                    <ContentControl name="ExpanderContent" Visibility="Collapsed" GrID.Row="1" GrID.Column="1" GrID.ColumnSpan="14"></ContentControl>              </GrID>            </DockPanel>          </border>        </DataTemplate> <DataTemplate x:Key="nonEditable">      <border x:name="brdNonEditable" WIDth="auto" HorizontalAlignment="Stretch">        <DockPanel WIDth="auto" Height="25">          <GrID WIDth="auto" Height="25">            <GrID.ColumnDeFinitions>              <ColumnDeFinition WIDth="25" />              <ColumnDeFinition WIDth="25" />              <ColumnDeFinition WIDth="100" />              <ColumnDeFinition WIDth="100" />              <ColumnDeFinition WIDth="80" />              <ColumnDeFinition WIDth="110" />              <ColumnDeFinition WIDth="110" />              <ColumnDeFinition WIDth="60" />              <ColumnDeFinition WIDth="90" />            </GrID.ColumnDeFinitions>            <CheckBox x:name="chkActive" GrID.Column="7" Height="25" Style="{StaticResource editorCheckBox}" tooltip="Is Construction Active?" IsEnabled="false" ValIDation.ErrorTemplate="{StaticResource valIDationTemplate}">              <CheckBox.IsChecked>                <Binding Path="Active">                  <Binding.ValIDationRules>                    <DataErrorValIDationRule></DataErrorValIDationRule>                    <ExceptionValIDationRule></ExceptionValIDationRule>                  </Binding.ValIDationRules>                </Binding>              </CheckBox.IsChecked>            </CheckBox>            <Label Content="calCompDate" Style="{StaticResource editorLabelList}" GrID.Column="8" tooltip="{Binding Path= CompDate}" />          </GrID>        </DockPanel>      </border>    </DataTemplate>

非常感谢所有试图帮助我解决这个问题的人!

解决方法 尝试在ListBox上将ScrollVIEwer.Horizo​​ntalScrollbarVisibility属性设置为“Disabled”.这迫使物品容器具有固定的宽度;否则,它们可以具有任何水平尺寸,并且不能合理地计算水平对齐.

根据Donnelle的回答,垂直对齐应该是修改ListBoxItem样式的问题.

编辑:在您的代码片段中,CheckBox位于一个位于边框内的DockPanel内的网格内.您想要准确居中哪个元素?你确定其余的都没有干涉吗?以下是我的建议和Horizo​​ntalContentAlignment =“Center”的查找方式,以及数据模板中的复选框:

还有一个编辑:我完全按照您粘贴的片段中的方式复制/粘贴网格/停靠面板/边框,结果完全相同 – 项目水平居中.

总结

以上是内存溢出为你收集整理的如何在ListBoxItem中居中WPF CheckBox全部内容,希望文章能够帮你解决如何在ListBoxItem中居中WPF CheckBox所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1058559.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存