繁体   English   中英

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

[英]How do I bind to a property of an object under a collection of collections?

我有一个ObservableCollection包含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;
            }
        }
    }
}

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

由于您的topList具有“ topList的集合,因此您需要某种嵌套项控件来迭代“ 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>

如果BottomList中只有一项,则可以对Button使用以下代码

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

如果要将BottomList绑定到某些列表控件,则可以绑定到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>

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

暂无
暂无

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

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