21

以下は、私が達成しようとしていることと似ています。ただし、エラーが発生します

PropertyDescriptor の値が無効です。

テンプレート上Setter。;に aTargetTypeを指定しなかったためだと思います。Styleただし、 のコンテナ タイプがわかりませんItemsControl

<ItemsControl>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <TextBlock Text="Some Content Here" />
                            <ContentPresenter />
                            <Button Content="Edit" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <!-- heterogenous controls -->
    <ItemsControl.Items> 
        <Button Content="Content 1" />
        <TextBox Text="Content 2" />
        <Label Content="Content 3" />
    </ItemsControl.Items>
</ItemsControl>
4

2 に答える 2

43

プロパティ名を型名で修飾できます。

<Setter Property="Control.Template">

のコンテナItemsControlは通常ContentPresenterですが、子が の場合はUIElementコンテナを使用しません。この場合、すべての子がコントロールであるため、ItemContainerStyleがそれらに直接適用されます。以外のアイテムを追加した場合UIElement、そのセッターは にControl.Templateプロパティを設定しますContentPresenter。これは成功しますが、効果はありません。

実際には、すでにUIElement. そのためには、 のサブクラスを使用する必要がありますItemsControl。のような既存のものを使用することもListBox、サブクラス化ItemsControlしてオーバーライドGetContainerForItemOverrideIsItemItsOwnContainerOverride、アイテムを独自のコンテナーにラップすることもできます。それらを でラップし、ContentControlそれを の として使用TargetTypeできStyleます。

public class CustomItemsControl
    : ItemsControl
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new ContentControl();
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        // Even wrap other ContentControls
        return false;
    }
}

また、 がプロパティにバインドされるTargetTypeようControlTemplateにを設定する必要があります。ContentPresenterContent

<ControlTemplate TargetType="ContentControl">
于 2010-08-22T17:04:36.803 に答える
3

また、すべてを XAML でのみ行いたい場合は、ItemsControl の代わりに ListBox を使用して、ListBoxItem のスタイルを定義するだけです。

        <ListBox ItemsSource="{Binding Elements.ListViewModels}">
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <StackPanel>
                                <TextBlock>Some Content Here</TextBlock>
                                <ContentPresenter Content="{TemplateBinding Content}" />
                                <Button>Edit</Button>
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.Resources>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

私は ListBox を使用しているため、コンテナーは ListBoxItem (通常、WPF の既定のリスト コントロールのコンテナーは常に Item という名前です) であるため、ListBoxItem のスタイルを作成することに注意してください。

<Style TargetType="ListBoxItem">

次に、ListBoxItem の新しい ControlTemplate を作成します。ContentPresenter は記事やチュートリアルに常に表示されるため使用されないことに注意してください。ListBoxItem の Content プロパティにテンプレート バインドする必要があるため、そのアイテムのコンテンツが表示されます。

<ContentPresenter Content="{TemplateBinding Content}" />

私はちょうど同じ問題を抱えていて、このように修正しました。ListBox (アイテムの選択) の一部の機能は必要ありません。この手法を使用すると、アイテムの選択が機能しなくなります。

于 2011-09-19T03:32:45.993 に答える