【问题标题】:The property content is set more than once.. WPF属性内容设置不止一次.. WPF
【发布时间】:2014-11-24 14:31:19
【问题描述】:

所以我和这个人有同样的问题..WPF: Bind Collection with Collection to a ListBox with groups

我尝试对我的问题实施建议的答案,但发现错误“属性'内容'设置不止一次”。唯一的区别是 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> 

非常感谢任何帮助!

【问题讨论】:

    标签: c# wpf xaml collections listbox


    【解决方案1】:

    您缺少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" 属性语法或&lt;Owner.Property&gt; 元素语法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多