无法绑定嵌套集合

时间:2013-11-19 20:40:50

标签: c# wpf xaml binding nested

我是WPF的初学者,我正在尝试绑定嵌套集合。

我在网上找到了很多关于绑定的主题,我尝试了this question/answer。我尝试更改Datacontext和ItemSource值,但我似乎无法正确使用它。

XAML:

<UserControl x:Class="WpfApplication1.Nav1"
         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="620" d:DesignWidth="950">
<Grid Background="#FF741125">
    <TextBlock Height="61" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="BEVERAGES" VerticalAlignment="Top" Width="250" FontSize="20" FontWeight="Black" />
    <ItemsControl x:Name="Stack" DataContext="{Binding myMenu}"  ItemsSource="{Binding Subs}" BorderThickness="0" Margin="0,60,0,0">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Name}" FontWeight="Bold" />
                    <ItemsControl ItemsSource="{Binding Nodes}" BorderThickness="0" Margin="0,60,0,0">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid Height="60" Width="900">
                                    <Grid.Background>
                                        <ImageBrush ImageSource="/WpfApplication1;component/Images/MenuGrid.fw.png" />
                                    </Grid.Background>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="10" />
                                        <RowDefinition Height="10" />
                                        <RowDefinition Height="10" />
                                        <RowDefinition Height="10" />
                                        <RowDefinition Height="10" />
                                        <RowDefinition Height="10" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                        <ColumnDefinition Width="50" />
                                    </Grid.ColumnDefinitions>

                                    <Label Content="{Binding Name}" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" Grid.ColumnSpan="14" FontWeight="Bold" />
                                    <Label Content="{Binding Description}" Grid.Column="1" Grid.Row="2" Grid.RowSpan="3" Grid.ColumnSpan="14" />
                                    <Label Content="{Binding Cost}" ContentStringFormat="{}${0}" Grid.Column="16" Grid.Row="0" Grid.RowSpan="5" Grid.ColumnSpan="2" />

                                </Grid>

                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

</Grid>

类别:

public class Menu
{
    public ObservableCollection<Category> Subs;

    public Menu()
    {
        Subs = new ObservableCollection<Category>();
    }
}

public class Category
{
    public ObservableCollection<MenuItem> Nodes;
    public Category()
    {
        Nodes = new ObservableCollection<MenuItem>();
    }
    private string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
    public Category(string name)
    {
        this.name = name;
        Nodes = new ObservableCollection<MenuItem>();
    }
}

public class MenuItem
{
    public MenuItem()
    {
    }

    public MenuItem(string name, string description, double cost)
    {
        this.itemName = name;
        this.description = description;
        this.cost = cost;
    }

    private string itemName;
    public string ItemName
    {
        get { return this.itemName; }
        set { this.itemName = value; }
    }

    private string description;
    public string Description
    {
        get { return this.description; }
        set { this.description = value; }
    }

    private double cost;
    public double Cost
    {
        get { return this.cost; }
        set { this.cost = value; }
    }
}

CS:

public Nav1()
    {
        InitializeComponent();
        // Init Model
        Menu myMenu = new Menu();
        myMenu.Subs.Add(new Category("Soft Drinks"));
        myMenu.Subs.Add(new Category("Coffee"));
        myMenu.Subs.Add(new Category("Premium"));

        myMenu.Subs[0].Nodes.Add(new MenuItem("Pepsi", "Cool & Refreshing", 1.39));
        myMenu.Subs[0].Nodes.Add(new MenuItem("Diet Pepsi", "Cool & Refreshing", 1.39));
        myMenu.Subs[0].Nodes.Add(new MenuItem("7Up", "Cool & Refreshing", 1.39));
        myMenu.Subs[0].Nodes.Add(new MenuItem("Mug Root Beer", "Cool & Refreshing", 1.39));
        myMenu.Subs[0].Nodes.Add(new MenuItem("Brisk Iced Tea", "Cool & Refreshing", 1.39));
        myMenu.Subs[0].Nodes.Add(new MenuItem("Bottled Water", "Thirsty? Aquafina.", 2.75));
        // Set DataContext for StackPanel
        Stack.DataContext = myMenu.Subs;
    }

显示类别名称(例如“软饮料”或“咖啡”),但我无法显示各个项目。此外,还有更好的方法吗?如上所述,我是WPF的初学者,似乎有许多我缺少的有趣功能。

感谢阅读!

1 个答案:

答案 0 :(得分:1)

使用DataTemplate不允许使用嵌套项,因为它不提供ItemsSource属性。

<DataTemplate>
    <TextBlock Text={Binding MyTextValue}/>
</DataTemplate>

但是,如果您使用HierarchicalDataTemplate,则可以使用ItemsSource属性来定义下一级别的项目。

<HierarchicalDataTemplate ItemsSource={Binding MyListOfLevelTwoItems}>
    <TextBlock Text={Binding MyLevelOneValue}/>
</HierarchicalDataTemplate>
<DataTemplate DataType={x:Type MyLevelTwoItem}>
    <TextBlock Text={Binding MyLevelTwoValue}/>
</DataTemplate>