Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to center a WPF CheckBox within a ListBoxItem
    primarykey
    data
    text
    <p>I have a ListBox that uses an ItemContainerStyle. I have tried everything I can think of to get a CheckBox control to center vertically and horizontally. Any ideas?</p> <pre><code>&lt;ListBox IsSynchronizedWithCurrentItem="True" Height="Auto" Width="Auto" DockPanel.Dock="Top" ItemContainerStyle="{StaticResource lbcStyle}" /&gt; &lt;Style TargetType="ListBoxItem" x:Key="lbcStyle"&gt; &lt;Style.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource editable}"/&gt; &lt;/Trigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="ContentTemplate" Value="{StaticResource nonEditable}"/&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Center"/&gt; '//i have tried stretch here also &lt;Setter Property="VerticalContentAlignment" Value="Stretch"/&gt; &lt;/Style&gt; </code></pre> <p>CheckBoxes get this style:</p> <pre><code>&lt;Style x:Key="editorCheckBox" TargetType="{x:Type CheckBox}"&gt; &lt;Setter Property="MinWidth" Value="67" /&gt; &lt;Setter Property="Height" Value="25" /&gt; &lt;Setter Property="Margin" Value="5,0,5,0" /&gt; &lt;Setter Property="VerticalAlignment" Value="Center" /&gt; &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt; &lt;/Style&gt; </code></pre> <p>Here are editable / non-editable:</p> <pre><code>&lt;DataTemplate x:Key="editable"&gt; &lt;Border x:Name="brdEditable" Width="Auto" HorizontalAlignment="Stretch"&gt; &lt;DockPanel x:Name="dpdEditable" LastChildFill="True" Width="Auto" Height="Auto"&gt; &lt;Grid x:Name="grdEditable" Width="Auto" Height="Auto"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="80" /&gt; &lt;ColumnDefinition Width="110" /&gt; &lt;ColumnDefinition Width="110" /&gt; &lt;ColumnDefinition Width="60" /&gt; &lt;ColumnDefinition Width="90" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition&gt;&lt;/RowDefinition&gt; &lt;RowDefinition&gt;&lt;/RowDefinition&gt; &lt;/Grid.RowDefinitions&gt; '... &lt;CheckBox x:Name="chkActive" Grid.Column="7" Height="25" Style="{StaticResource editorCheckBox}" ToolTip="Is Construction Active?" IsEnabled="true" Validation.ErrorTemplate="{StaticResource validationTemplate}"&gt; &lt;CheckBox.IsChecked&gt; &lt;Binding Path="Active"&gt; &lt;Binding.ValidationRules&gt; &lt;DataErrorValidationRule&gt;&lt;/DataErrorValidationRule&gt; &lt;ExceptionValidationRule&gt;&lt;/ExceptionValidationRule&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/CheckBox.IsChecked&gt; &lt;/CheckBox&gt; '... &lt;ContentControl Name="ExpanderContent" Visibility="Collapsed" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="14"&gt;&lt;/ContentControl&gt; &lt;/Grid&gt; &lt;/DockPanel&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="nonEditable"&gt; &lt;Border x:Name="brdNonEditable" Width="Auto" HorizontalAlignment="Stretch"&gt; &lt;DockPanel Width="Auto" Height="25"&gt; &lt;Grid Width="Auto" Height="25"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="25" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="100" /&gt; &lt;ColumnDefinition Width="80" /&gt; &lt;ColumnDefinition Width="110" /&gt; &lt;ColumnDefinition Width="110" /&gt; &lt;ColumnDefinition Width="60" /&gt; &lt;ColumnDefinition Width="90" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;CheckBox x:Name="chkActive" Grid.Column="7" Height="25" Style="{StaticResource editorCheckBox}" ToolTip="Is Construction Active?" IsEnabled="false" Validation.ErrorTemplate="{StaticResource validationTemplate}"&gt; &lt;CheckBox.IsChecked&gt; &lt;Binding Path="Active"&gt; &lt;Binding.ValidationRules&gt; &lt;DataErrorValidationRule&gt;&lt;/DataErrorValidationRule&gt; &lt;ExceptionValidationRule&gt;&lt;/ExceptionValidationRule&gt; &lt;/Binding.ValidationRules&gt; &lt;/Binding&gt; &lt;/CheckBox.IsChecked&gt; &lt;/CheckBox&gt; &lt;Label Content="calCompDate" Style="{StaticResource editorLabelList}" Grid.Column="8" ToolTip="{Binding Path= CompDate}" /&gt; &lt;/Grid&gt; &lt;/DockPanel&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; </code></pre> <p>And thanks so much to everyone who has tried to help me solve this!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload