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

C# 如何绑定到集合集合下对象的属性?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个包含name属性的类(TopLevel)的ObservableCollection,还有一个只包含name属性的类(BottomLevel)的另一个ObservableCollection。最高列表上的绑定是有效的,但是当我尝试绑定到底部列表上的属性时,我什么也得不到。我错过了什么 XAML: 代码隐藏: public partial class MainWindow : Window { public ObservableCollection<TopLevel>

我有一个包含name属性的类(TopLevel)的
ObservableCollection
,还有一个只包含name属性的类(BottomLevel)的另一个
ObservableCollection
。最高列表上的绑定是有效的,但是当我尝试绑定到底部列表上的属性时,我什么也得不到。我错过了什么

XAML:


代码隐藏:

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;
            }
        }
    }
}
公共部分类主窗口:窗口
{
公众可观察的收藏排行榜;
公共主窗口()
{
初始化组件();
topList=新的ObservableCollection();
添加(新的顶级(“T”+(1+topList.Count).ToString());
topList[0]。AddBottom();
添加(新的顶级(“T”+(1+topList.Count).ToString());
topList[1]。AddBottom();
ic1.ItemsSource=顶级列表;
}
}
公共类顶级
{
私人可观察收集清单;
私有字符串topName;
公共void AddBottom()
{
Add(新的BottomLevel(“B”+(1+bottomList.Count).ToString());
}
公共顶级(字符串x)
{
bottomList=新的ObservableCollection();
topName=x;
}
公共字符串名
{
得到
{
返回topName;
}
设置
{
if(topName!=值)
{
topName=值;
}
}
}
公共可观测集合底部列表
{
得到
{
返回底部列表;
}
设置
{
if(bottomList!=值)
{
底部列表=值;
}
}
}
}
公共阶级底层
{
私有字符串名称;
公共底层(字符串x)
{
bottomName=x;
}
公共字符串名称
{
得到
{
返回底部名称;
}
设置
{
if(bottomName!=值)
{
bottomName=值;
}
}
}
}

您的按钮路径不正确。底部列表没有“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>

//您的数据上下文现在是顶级项本身
//这里有什么?你有一整套底层物品可供选择!

您的按钮路径不正确。底部列表没有“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>

//您的数据上下文现在是顶级项本身
//这里有什么?你有一整套底层物品可供选择!

如果“底部列表”中只有一项,则可以使用下面的按钮代码

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

如果要将底部列表绑定到某个列表控件,可以绑定到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>



如果您需要更多帮助,请告诉我。

如果底部列表中只有一项,则可以使用下面的按钮代码

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

如果要将底部列表绑定到某个列表控件,可以绑定到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>



如果需要更多帮助,请告诉我。

是否要绑定到BottomList中第一个BottomLevel的BottomName?{Binding BottomList/BottomName}在这种情况下可能有效。是否要绑定到BottomList中第一个BottomLevel的BottomName?{Binding BottomList/BottomName}在这种情况下可能有效。