1

データバインドされたListBoxを取得して、ListBoxItemのテンプレート化されたスタイル(それぞれのスタイルと同じ名前のResourceDictionary内から)を受け入れるにはどうすればよいですか?

Blend 4で、SimpleStyles ResourceDictionaryファイル内で、「SimpleListBoxItem」のプロパティが次のように設定されていることがわかります。

 d:IsControlPart="True"

しかし、これは、xamlのハードコードされたListBoxItemsにSimpleListBoxItemスタイルを明示的に使用する場合にのみ使用できますか?

私にとって理にかなっているのは、リストボックス内のControlTemplateにスタイルを適用することです。リストボックス内のコントロールテンプレートは次のようになります。

ControlTemplate TargetType="{x:Type ListBox}">
                <Grid>
                    <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            Background="{TemplateBinding Background}"
                            />
                    <ScrollViewer Margin="1" Style="{DynamicResource SimpleScrollViewer}" Focusable="false" Background="{TemplateBinding Background}">

                        <!-- The StackPanel is used to display the children by setting IsItemsHost to be True -->
                        <StackPanel Margin="2" IsItemsHost="true"/>

                    </ScrollViewer>
                </Grid>

そのスタックパネル内にもう1つのネストされた「ItemsHost」スタイルテンプレートを配置する方法はありますか?多分DataTemplate?

よろしくお願いします。さらに詳しい説明が必要な場合はお知らせください。

4

1 に答える 1

3

ListBoxスタイル 内からアイテムにスタイルを適用するには、2つのオプションがありItemContainerStyleますItemTemplate

1)ItemContainerStyleタイプに適用されListBoxItemます-リスト内の各アイテムのコンテナのスタイルを設定します。

<Style TargetType="ListBoxItem" x:Key="SimpleListBoxItem">
    <Setter Property="Background" Value="Green">
    <!-- etc -->
</Style>

<Style TargetType="ListBox" x:Key="ListBoxStyle">
    <Setter Property="ItemContainerStyle" Value="{StaticResource SimpleListBoxItem}">
</Style>

2)ItemTemplateプロパティを使用すると、各アイテムの表示方法についてテンプレートを完全に再定義できます。例:

<Style TargetType="ListBox" x:Key="ListBoxStyle">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="This is an item" />
                    <ContentControl Grid.Column="1" Text="{Binding}" />
                <Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2012-12-12T19:54:22.970 に答える