如何绑定到集合集合下的对象的属性?

时间:2014-04-29 23:15:06

标签: c# wpf xaml

我有一个ObservableCollection类(TopLevel),它包含一个name属性和一个只有name属性的另一个ObservableCollection类(BottomLevel)的列表。最高列表上的绑定有效,但是当我尝试绑定到BottomList上的属性时,我什么也得不到。我在这里缺少什么?

XAML:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myGrid" DataContext="topList">
    <Border BorderBrush="AliceBlue" Grid.Column="0" BorderThickness="5">
        <ItemsControl x:Name="ic1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Path=TopName}"/>
                        <Border BorderBrush="AntiqueWhite" Grid.Column="1" BorderThickness="5">
                            <Button Content="{Binding Path=BottomList.BottomName}"/>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>                
        </ItemsControl>            
    </Border>
</Grid>

代码背后:

public partial class MainWindow : Window
{
    public ObservableCollection<TopLevel> topList;
    public MainWindow()
    {
        InitializeComponent();
        topList = new ObservableCollection<TopLevel>();
        topList.Add(new TopLevel("T" + (1 + topList.Count).ToString()));
        topList[0].AddBottom();
        topList.Add(new TopLevel("T" + (1 + topList.Count).ToString()));
        topList[1].AddBottom();
        ic1.ItemsSource = topList;            
    }
}

public class TopLevel
{
    private ObservableCollection<BottomLevel> bottomList;
    private string topName;

    public void AddBottom()
    {
        bottomList.Add(new BottomLevel("B" + (1 + bottomList.Count).ToString()));
    }

    public TopLevel(string x)
    {
        bottomList = new ObservableCollection<BottomLevel>();
        topName = x;
    }

    public string TopName
    {
        get
        {
            return topName;
        }
        set
        {
            if (topName!=value)
            {
                topName = value;
            }
        }
    }

    public ObservableCollection<BottomLevel> BottomList
    {
        get
        {
            return bottomList;
        }
        set
        {
            if (bottomList!=value)
            {
                bottomList = value;
            }
        }
    }
}

public class BottomLevel
{
    private string bottomName;

    public BottomLevel(string x)
    {
        bottomName = x;
    }

    public string BottomName
    {
        get
        {
            return bottomName;
        }
        set
        {
            if (bottomName!=value)
            {
                bottomName = value;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您的按钮路径不正确。 BottomList没有“Name”属性,因此您无法绑定它。相反,只需使用BottomName作为路径。

由于您的topList有一个“BottomLevel”集合,您需要某种嵌套项控件来迭代“bottomList”集合(然后使用“BottomName”作为您的路径)。

目前,你基本上有:

<ItemsControl //List of TopLevel>
//Your data context is now the TopLevel item itself
<Button Path=/> //What goes here? you have a whole collection of BottomLevel items to choose from!
</ItemsControl>

答案 1 :(得分:1)

如果BottomList中只有一个项目,那么您可以使用以下代码进行按钮

 <Button Content="{Binding Path=BottomList[0].BottomName}" Height="50"/>

如果你想将BottomList绑定到某个List Control,你可以绑定到DataGrid,那么你可以使用下面的代码。

<Grid x:Name="myGrid" DataContext="topList">
    <Border BorderBrush="AliceBlue" Grid.Column="0" BorderThickness="5">
        <ItemsControl x:Name="ic1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Path=TopName}"/>
                        <Border BorderBrush="AntiqueWhite" Grid.Column="1"    BorderThickness="5">
                            <DataGrid ItemsSource="{Binding Path=BottomList}" AutoGenerateColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTemplateColumn>
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Button Content="{Binding Path=BottomName}" Height="50"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                          </Border>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Border>
</Grid>

如果您需要更多帮助,请与我们联系。