2010-09-20 97 views
1

我有一個問題,拉伸ListBoxItem的內容。我使用DataTemplateGrid將最後一列的內容放在右側。但是我必須擁有防止這種顯示的控件的基本風格 - 「*」(「消耗所有剩餘的空間」)顯示爲「自動」(「只取決於你真正需要的」)。所有ListBox ES的拉伸ListBoxItem樣式問題

風格:

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

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> 

類別應留待,電話&手機應該是在右側,名稱&位置應該充滿整個空間離開了。這似乎是項目中沒有「整體」空間,儘管它們在光學上填充了列表框寬度的空間。

任何人都可以幫助我嗎?我對此感到生氣。 :-(

編輯:Picture

回答

1

嘗試將Horizo​​ntalContentAlignment爲ListBoxItem的爲Stretch有點像一個in this link除了使用「列表框」和「一個ListBoxItem」:

+0

我很抱歉問了一個已經現在回答的問題,我真的認爲,我已經設置Horizo​​ntalContentAlignment到處伸展,但我確實沒有做這個項目,非常感謝! – Amaryllion 2010-09-20 14:02:16

+0

沒有問題。=) – ASanch 2010-09-20 14:11:24