views:

20

answers:

1

I have a problem stretching the content of a ListBoxItem. I use a DataTemplate with a Grid to place the content of the last column aligned at the right. But I must have something in the basic style of the controls that prevents this kind of display - the "*" ("consume all the rest of space") displays like "auto" ("take only what you really need").

Style of all ListBoxes:

<Style TargetType="{x:Type ListBox}">
  <Setter Property="SnapsToDevicePixels" Value="true" />
  <Setter Property="Background" Value="{DynamicResource WindowBackgroundBrush}" />
  <Setter Property="BorderBrush" Value="{DynamicResource SolidBorderBrush}" />
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
  <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type ListBox}">
     <Grid HorizontalAlignment="Stretch">
                        <Border x:Name="Border" HorizontalAlignment="Stretch" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" />
      <ScrollViewer Margin="1" Style="{DynamicResource NuclearScrollViewer}" Focusable="false" Background="{DynamicResource LightBrush}" x:Name="scrollViewer">
       <StackPanel Margin="2" IsItemsHost="true" HorizontalAlignment="Stretch" />
      </ScrollViewer>
     </Grid>
     <ControlTemplate.Triggers>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border" />
       <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border" />
       <Setter Property="Background" TargetName="scrollViewer" Value="{DynamicResource DisabledBackgroundBrush}"/>
      </Trigger>
      <Trigger Property="IsGrouping" Value="true">
       <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
      </Trigger>
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
 <Style d:IsControlPart="True" TargetType="{x:Type ListBoxItem}">
  <Setter Property="SnapsToDevicePixels" Value="true" />
  <Setter Property="OverridesDefaultStyle" Value="true" />
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid SnapsToDevicePixels="true" HorizontalAlignment="Stretch"  >
                        <Border HorizontalAlignment="Stretch" x:Name="Border" Opacity="0.25" Margin="0,1,0,1" Background="{DynamicResource NormalBrush}" BorderBrush="{DynamicResource NormalBorderBrush}" BorderThickness="1,1,1,1" CornerRadius="0,0,0,0" Padding="0,0,0,0" />
      <Rectangle Opacity="0.25" Fill="{DynamicResource LightBrush}" Stroke="{x:Null}" Height="10.849" Margin="1.153,1.151,1,0" VerticalAlignment="Top" />
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="5,2,0,2" x:Name="contentPresenter" />
     </Grid>
     <ControlTemplate.Triggers>
      <MultiTrigger>
       <MultiTrigger.Conditions>
        <Condition Property="Selector.IsSelected" Value="True"/>
        <Condition Property="IsEnabled" Value="False"/>
       </MultiTrigger.Conditions>
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource DisabledBackgroundBrush}"/>
       <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource DisabledBorderBrush}"/>
      </MultiTrigger>
      <Trigger Property="IsSelected" Value="true">
       <Setter Property="Opacity" TargetName="Border" Value="1" />
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource SelectedBackgroundBrush}" />
      </Trigger>
      <MultiTrigger>
       <MultiTrigger.Conditions>
        <Condition Property="IsMouseOver" Value="True" />
        <Condition Property="Selector.IsSelected" Value="False" />
       </MultiTrigger.Conditions>
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource MouseOverBrush}" />
       <Setter Property="Opacity" TargetName="Border" Value="1" />
      </MultiTrigger>
      <MultiTrigger>
       <MultiTrigger.Conditions>
        <Condition Property="Selector.IsSelected" Value="True" />
        <Condition Property="IsMouseOver" Value="True" />
       </MultiTrigger.Conditions>
       <Setter Property="Opacity" TargetName="Border" Value="0.65" />
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource SelectedBackgroundBrush}" />
      </MultiTrigger>
      <MultiTrigger>
       <MultiTrigger.Conditions>
        <Condition Property="IsSelected" Value="true" />
        <Condition Property="Selector.IsSelectionActive" Value="false" />
       </MultiTrigger.Conditions>
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource SelectedBackgroundBrush}" />
       <Setter Property="Opacity" TargetName="Border" Value="0.6" />
      </MultiTrigger>
      <Trigger Property="IsEnabled" Value="false">
       <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}" />
       <Setter Property="Background" TargetName="Border" Value="{DynamicResource DisabledBackgroundBrush}"/>
       <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource DisabledBorderBrush}"/>
      </Trigger>
     </ControlTemplate.Triggers>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>

My ListBox:

<ListBox Height="220"
                            DataContext="{Binding}"
                            ItemsSource="{Binding Persons}"
                            SelectedItem="{Binding SelectedPerson}"
                            VirtualizingStackPanel.VirtualizationMode="Recycling" 
                            VirtualizingStackPanel.IsVirtualizing="True"
                            ScrollViewer.IsDeferredScrollingEnabled="True" 
                            HorizontalAlignment="Stretch"
                            HorizontalContentAlignment="Stretch" 
                            ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                            ScrollViewer.VerticalScrollBarVisibility="Auto"
                            ScrollViewer.CanContentScroll="True">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid 
                                    HorizontalAlignment="Stretch" 
                                    MaxWidth="{Binding
                                        RelativeSource={RelativeSource
                                        Mode=FindAncestor,
                                        AncestorType={x:Type ListBox}},
                                        Path=ActualWidth}">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" 
                                            Width="25" Height="25"
                                            Margin="0,0,5,0" 
                                            HorizontalAlignment="Right"
                                            VerticalAlignment="Center"
                                            Text="{Binding Path=BusinessDataObject.Category}">
                                            <TextBlock.Style>
                                                <Style TargetType="{x:Type TextBlock}">
                                                    <Setter Property="TextAlignment" Value="Center"/>
                                                    <Setter Property="FontSize" Value="16"/>
                                                    <Setter Property="Foreground" Value="White"/>
                                                    <Setter Property="Background" Value="Transparent"/>
                                                    <Style.Triggers>
                                                        <Trigger Property="Text" Value="A">
                                                            <Setter Property="Background" Value="Red"/>
                                                        </Trigger>
                                                        <Trigger Property="Text" Value="B">
                                                            <Setter Property="Background" Value="Orange"/>
                                                        </Trigger>
                                                        <Trigger Property="Text" Value="C">
                                                            <Setter Property="Background" Value="Blue"/>
                                                        </Trigger>
                                                    </Style.Triggers> 
                                                </Style>
                                            </TextBlock.Style>
                                    </TextBlock>
                                    <TextBlock 
                                            Grid.Row="0"
                                            Grid.Column="1" 
                                            Margin="0,0,10,0"
                                            HorizontalAlignment="Left" 
                                            TextWrapping="Wrap"
                                            FontWeight="Bold"
                                            Text="{Binding Path=BusinessDataObject.FullNameReversed}"/>
                                    <TextBlock 
                                            Grid.Row="1" Grid.Column="1" Margin="0,0,10,0"
                                            HorizontalAlignment="Left" 
                                            Text="{Binding Path=BusinessDataObject.Position}"/>
                                    <TextBlock 
                                            Grid.Row="0" Grid.Column="2" Margin="0,0,0,0"
                                            HorizontalAlignment="Right"
                                            TextAlignment="Left"
                                            Text="{Binding Path=BusinessDataObject.Phone}"/>
                                    <TextBlock 
                                            Grid.Row="1" Grid.Column="2" Margin="0,0,0,0"
                                            HorizontalAlignment="Right"
                                            TextAlignment="Left"
                                            Text="{Binding Path=BusinessDataObject.Mobile}"/>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>   
                    </ListBox>

Category should be left, Phone & Mobile should be on the right and Name & Position should fill the whole space that is left. It seems to be that there is no "whole" space within the Items although they optically fill the space of the listboxes width.

Could anyone help me? I'm getting mad about this. :-(

Edit: Picture

A: 

Try setting the HorizontalContentAlignment for the ListBoxItem to Stretch. Something like the one in this link except using "ListBox" and "ListBoxItem":

karmicpuppet
I am so sorry having asked an already existing answered question, I really thought, I had set HorizontalContentAlignment to stretch everywhere but I did indeed not do it for the Item. Thank you very much!
Amaryllion
Not a problem. =)
karmicpuppet