2011-06-12 10 views
2

WrapPanelのListBoxでItemsPanelを交換しようとしていますが、そのスタイルのItemsPanelTemplateは効果がないようです。境界線と背景色が変わるためスタイルが見つけられ適用されますが、スヌープで検査するとWrapPanelは表示されません。ItemsPanelテンプレートは無効です

<Style x:Key="CocktailGrid" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="Background" Value="White" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="BorderBrush" Value="Black" /> 
    <Setter Property="OverridesDefaultStyle" Value="true" /> 
    <Setter Property="SelectionMode" Value="Single" /> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <WrapPanel 
        IsItemsHost="True" 
        Width="{Binding 
           Path=ActualWidth, 
           RelativeSource={RelativeSource 
            Mode=FindAncestor, 
            AncestorType= 
            {x:Type ScrollContentPresenter}}}" /> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="80" /> 
         <RowDefinition Height="*"/> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <Image Grid.Row="0" Source="{Binding ImageName}" Height="80" Stretch="Uniform"/> 
        <TextBlock Grid.Row="1" Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/> 
       </Grid> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

リストボックスは次のように宣言されています

<ListBox x:Name="lstCocktails" PreviewKeyDown="dg_PreviewKeyDown" ItemsSource="{Binding Source={StaticResource drinksSource}}" SelectedItem="{Binding SelectedItem,ElementName=root,Mode=TwoWay}" Style="{StaticResource CocktailGrid}" 
      SelectionMode="Single" MouseDoubleClick="lstCocktails_MouseDoubleClick"> 

スヌープビジュアルツリー: Snoop visual tree

私はアプリの他の部分でItemsPanelsをオーバーライドしてきましたが、何らかの理由でこの1つは私を逃亡されます

答えて

1

ItemsTemplateの代わりにthisを使用してください。

 <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBox"> 
       <Border Background="{TemplateBinding ListBox.Background}" CornerRadius="5"> 
        <WrapPanel IsItemsHost="True"/> 
       </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 

働いています

+0

ありがとうございます。正常に動作します。 – Echilon

関連する問題