
<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.HorizontalScrollbarVisibility属性设置为“Disabled”.这迫使物品容器具有固定的宽度;否则,它们可以具有任何水平尺寸,并且不能合理地计算水平对齐.根据Donnelle的回答,垂直对齐应该是修改ListBoxItem样式的问题.
编辑:在您的代码片段中,CheckBox位于一个位于边框内的DockPanel内的网格内.您想要准确居中哪个元素?你确定其余的都没有干涉吗?以下是我的建议和HorizontalContentAlignment =“Center”的查找方式,以及数据模板中的复选框:
还有一个编辑:我完全按照您粘贴的片段中的方式复制/粘贴网格/停靠面板/边框,结果完全相同 – 项目水平居中.
总结以上是内存溢出为你收集整理的如何在ListBoxItem中居中WPF CheckBox全部内容,希望文章能够帮你解决如何在ListBoxItem中居中WPF CheckBox所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)