1

カスタムスタイルでスタイリングしたいComboBoxがあります。必要なスタイルを提供することに成功しました。スタイルでは、私は次の要素を持っています:

  • トグルボタンのテンプレート
  • ComboBoxのアイテムを保持するポップアップ

私が参照したほとんどすべてのリンクは、同じアプローチを使用していました。しかし、このアプローチでは、ComboBox内のアイテムのテンプレートを提供できません。何らかの理由で、私が定義しているアイテムテンプレートは、アイテムのレンダリングに使用されていません。誰かが私を助けてくれますか?問題の説明を明確にするためにコードのサンプルを貼り付けています(コードに間違いがある可能性があります。アイデアを進めたいだけです)。

 <Window.Resources>
    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <ToggleButton Template="{StaticResource MyToggleButton}"/>
                        <Popup >
                            <StackPanel>
                                <Border/>
                                <ItemsPresenter/>
                            </StackPanel>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ComboBox Style="{StaticResource MyStyle}">
        <ComboBox.ItemTemplate>
            <DataTemplate>

            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>
4

1 に答える 1

2

ComboBoxItemコントロールに別のControlTemplateを提供しようとしている場合は、ComboBoxのItemContainerStyleプロパティを、親コントロールで既に行ったものと同様のスタイルに設定する必要がありますが、ComboBoxItem用です。ItemTemplateは、各アイテムのデータに適用するDataTemplateを定義します。このデータは、そのComboBoxItemControlTemplateに挿入されます。

<ComboBox Style="{StaticResource MyStyle}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border>
                            <ContentPresenter/> <!--ItemTemplate is injected here-->
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ComboBox.ItemContainerStyle>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=SomePropertyOnMyDataObject}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
于 2013-02-04T18:29:53.980 に答える