为ItemsControl.ItemContainerStyle指定ControlTemplate [英] Specify ControlTemplate for ItemsControl.ItemContainerStyle

查看:417
本文介绍了为ItemsControl.ItemContainerStyle指定ControlTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下内容与我要完成的操作类似.但是,我得到了错误

The following is similar to what I'm trying to accomplish. However, I get the error

无效的PropertyDescriptor值.

Invalid PropertyDescriptor value.

模板Setter上的

.我怀疑是因为我没有为Style指定TargetType;但是,我不知道ItemsControl的容器类型.

on the Template Setter. I suspect it's because I didn't specify a TargetType for the Style; however, I don't know the container type for 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>

推荐答案

您可以使用类型名称来限定属性名称:

You can qualify the property name with the type name:

<Setter Property="Control.Template">

ItemsControl的容器通常是ContentPresenter,但是如果子级是UIElement,则它将不使用容器.在这种情况下,所有子级都是控件,因此ItemContainerStyle将直接应用于它们.如果添加了UIElement以外的其他项目,则该设置程序将在ContentPresenter上设置Control.Template属性,该属性可以成功但无效.

The container for ItemsControl is normally a ContentPresenter, but if the child is a UIElement then it won't use a container. In this case, all of the children are Controls, so the ItemContainerStyle will apply to them directly. If you added an item other than a UIElement, that setter would set the Control.Template property on the ContentPresenter, which would succeed but have no effect.

实际上,听起来每个孩子都想将其包装在一个容器中,即使它们已经是UIElement.为此,您将必须使用ItemsControl的子类.您可以使用现有的类似ListBox的子类,也可以子类化ItemsControl并覆盖

Actually, it sounds like what you want is to wrap each child in a container, even if they are already a UIElement. To do that, you will have to use a subclass of ItemsControl. You could use an existing one like ListBox, or you could subclass ItemsControl and override GetContainerForItemOverride and IsItemItsOwnContainerOverride to wrap the items in your own container. You could wrap them in a ContentControl and then use that as the TargetType for the Style.

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

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

您还需要在ControlTemplate上设置TargetType,以便ContentPresenter绑定到Content属性:

You will also need to set the TargetType on the ControlTemplate so that the ContentPresenter will bind to the Content property:

<ControlTemplate TargetType="ContentControl">

这篇关于为ItemsControl.ItemContainerStyle指定ControlTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆