代码之家  ›  专栏  ›  技术社区  ›  Travis Heseman

为指定ControlTemplateItemsControl.ItemContainerStyle

  •  19
  • Travis Heseman  · 技术社区  · 16 年前

    无效的PropertyDescriptor值。

    在模板上 Setter . 我怀疑是因为我没有指明 TargetType 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>
    
    2 回复  |  直到 11 年前
        1
  •  42
  •   Jogge Rob Parker    7 年前

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

    <Setter Property="Control.Template">
    

    容器 ItemsControl ContentPresenter ,但如果孩子是 UIElement 那它就不用容器了。在本例中,所有子对象都是控件,因此 ItemContainerStyle UIElement元素 ,那个设定者会设定 Control.Template 上的属性 任命者

    实际上,听起来你想要的是把每个孩子都包装在一个容器里,即使他们已经是一个孩子了 . 为此,必须使用 项目控件 . 你可以用现有的 ListBox ,或者可以将 和覆盖 GetContainerForItemOverride 和 IsItemItsOwnContainerOverride 用你自己的容器包装物品。你可以把它们包起来 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;
        }
    }
    

    您还需要设置 目标类型 上 ControlTemplate 任命者 将绑定到 Content

    <ControlTemplate TargetType="ContentControl">
    
        2
  •  3
  •   000    15 年前

    另外,如果您只想使用XAML完成所有操作,您可以简单地使用ListBox而不是ItemsControl,并为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>
    

    <Style TargetType="ListBoxItem">
    

    然后我们为ListBoxItem创建一个新的ControlTemplate。请注意,ContentPresenter并不是像在文章和教程中经常出现的那样使用的,您需要将其模板绑定到ListBoxItem的Content属性,这样它将显示该项目的内容。

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

    我也遇到了同样的问题,就这样解决了。我不想要一些功能的列表框(项目选择)和使用这种技术的项目选择不再工作。