繁体   English   中英

属性内容设置了一次以上。WPF

[英]The property content is set more than once.. WPF

所以我有与此人相同的问题。WPF:将具有Collection的Collection绑定到具有组的ListBox

我试图实现对问题的建议答案,但发现错误“属性'Content'设置不止一次”。 唯一的区别是id像这样在用户控件中实现:

这是我的C#代码:

public class Dog
{
    public int dogID { get; set; }
    public String Name { get; set; }

    List<Puppy> puppies { get; set; }
}

public class Puppy
{
    public String Name { get; set; }
}

这是XAML:

<UserControl x:Class="PetStore.Menu"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
            mc:Ignorable="d" 
            d:DesignHeight="300" d:DesignWidth="300">

    <CollectionViewSource x:Name="dogs" Source="{Binding}" >
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Name" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

    <DataTemplate x:Key="dogTemplate" DataType="Project">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}" />
        </StackPanel>
    </DataTemplate>

    <ListBox ItemsSource="{Binding Source={StaticResource dogs}}">
        <ListBox.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource dogTemplate}" />
        </ListBox.GroupStyle>

        <ListBox.ItemTemplate>
            <DataTemplate DataType="Puppy">
                <ListBox ItemsSource="{Binding puppies}">
                    <ListBox.ItemTemplate>
                        <DataTemplate DataType="Puppy">
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl> 

任何帮助是极大的赞赏!!

您缺少UserControl.Resources标记:

<UserControl x:Class="Tackle.View.Menu"
             ...>

    <UserControl.Resources> <!-- You're missing this -->

        <CollectionViewSource x:Key="dogs" Source="{Binding}" > <!-- Change x:Name to x:Key here -->
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Name" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <DataTemplate x:Key="dogTemplate" DataType="Project">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

   <!-- here goes your window content -->

错误消息告诉您您做错了什么:您试图多次设置Content属性。 当您将元素声明为控件的直接子级时,则隐式设置了在该控件类型的[ContentProperty("...")]属性中指定的任何属性。 对于ContentControl (及其子类UserControl ),该属性为Content 如果要设置其他属性,或向集合/字典属性添加某些内容,则必须指定属性名称。 这意味着要么使用Property="value"属性语法,要么使用<Owner.Property>元素语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM