views:

2151

answers:

5

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?

<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 get this style:

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

Here are editable / non-editable:

<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>

And thanks so much to everyone who has tried to help me solve this!

A: 

I've had the same issue to. Have you tried setting "HorizontalAlignment" directly on the ListBoxItem in the style? (not HorizontalContentAlignment)

Micah
Tried HorizontalAlignment=Center and Stretch. Neither had any effect.
Dave Lowther
A: 

Have you tried setting HorizontalContentAlignment to "Stretch" on the ListBox itself? I believe this is necessary to make each ListBoxItem fill the width of the ListBox.

Matt Hamilton
Tried HorizontalContentAlignment=Center and Stretch. Neither had any effect.
Dave Lowther
A: 

Setting the height on the ListBoxItem style-- rather than the checkbox-- does what I think you're after.

Donnelle
Can you be more specific?
Dave Lowther
A: 

The checkbox is aligned top-left. For a quick and dirty, I have updated the margin on the checkbox to 4,3,0,0 on a row heigh of 20. May need to be adjusted depending upon your row height and if you want a buffer on the left. The margin attribute can get you out of odd format situations if you don't have time to write your own template or use other controls/containers.

LD
+1  A: 

Try setting the ScrollViewer.HorizontalScrollBarVisibility property to "Disabled" on the ListBox. This forces the item containers to have a fixed width; otherwise, they can have any horizontal size, and horizontal alignment cannot be calculated sensibly.

Vertical alignment should be a matter of modifying the ListBoxItem style, as per Donnelle's answer.

Edit: In your code snippets, the CheckBox is inside a Grid which is inside a DockPanel which is inside a Border. Which element are you trying to center exactly? Are you sure the rest of them don't interfere? Here's how it looks for me with my suggestion and HorizontalContentAlignment="Center", and only the checkbox in the data template:

alt text One more edit: I copy/pasted your grid/dockpanel/border exactly as they appear in the snippets you pasted, and the result is exactly the same - items centered horizontally.

Alex Paven
Tried this. No change. Maybe I am just unlucky?
Dave Lowther