如何分配 ItemsSource 属性

How to assign ItemsSource property

好的,对不起我之前的混乱。

情况是这样的: 我有两个自定义对象定义如下: 主要对象:

public class MainObject
{
    private string mainObjectName;
    public string MainObjectName { get { return mainObjectName; } }

    private List<SubObject> subObjectData;
    public List<SubObject> SubObjectData { get { return subObjectData; } }

    public MainObject(string name, List<SubObject> objectData)
    {
        mainObjectName = name;
        subObjectData = objectData;
    }
}

子对象:

public class SubObject
{
    private string subObjectName;
    public string SubObjectName { get { return subObjectName; } }

    private List<int> integerData;
    public List<int> IntegerData { get { return integerData; } }

    public SubObject(string name, List<int> data)
    {
        subObjectName = name;
        integerData = data;
    }
}

我还有一个视图模型,为了简单起见,它使用这两个对象定义了一些数据 follows:VM

public List<Model.MainObject> VMList = new List<Model.MainObject>()
    {
        new Model.MainObject("MainItem1", new List<Model.SubObject>()
        {
            new Model.SubObject("SubItem1", new List<int>() { 1,6,3}),
            new Model.SubObject("SubItem2", new List<int>() { 5,2,9})
        }),
        new Model.MainObject("MainItem2", new List<Model.SubObject>()
        {
            new Model.SubObject("SubItem1", new List<int>() { 0,3,1}),
            new Model.SubObject("SubItem2", new List<int>() { 7,5,2})
        })
    };

现在我有以下UI

<Grid>
    <ItemsControl Name="MainObjectIC">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding MainObjectName}"/>
                    <ItemsControl Name="SubObjectIC">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding SubObjectName}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

我在后面的代码中分配了 MainObjectIC 的 ItemsSource:

ViewModel.VM dc = new ViewModel.VM();

    public MainWindow()
    {
        InitializeComponent();
        DataContext = dc;
        MainObjectIC.ItemsSource = dc.VMList;
    }

我还想将 ItemsSource 分配给 SubObjectIC,但为此我必须获取 ItemsControl 对象。这就是我想要实现的目标。

据我所知,从后面的代码中分配 ItemsSource 属性 可能是非常非常糟糕且无用的。

感谢您改进代码示例。它仍然不是很完整,但已经足够接近能够提供答案了。

在您的示例中,缺少的主要内容是简单地添加必要的 {Binding} 表达式。特别是:

<ItemsControl Name="SubObjectIC" Grid.Column="1"
              ItemsSource="{Binding SubObjectData}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding SubObjectName}"/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

该项目的上下文已经是一个类型为 MainObject 的对象(这就是为什么您的 TextBlock 绑定有效)。所以剩下要做的就是将 ItemsSource 属性 绑定到 MainObject.SubObjectData 属性.

(我必须添加 Grid.Column 赋值,上面的示例中似乎缺少该赋值。)

以上更改完全足以让您的示例按您的意愿工作。但是,您也可以通过对顶级控件使用相同的基本方法来改进代码。为此,您的 VM.VMList 字段需要更改为 属性(WPF 仅绑定到属性,而不是字段):

class VM
{
    public List<MainObject> VMList {  get { return _vmList; } }

    private readonly List<MainObject> _vmList = new List<MainObject>()
    {
        new MainObject("MainItem1", new List<SubObject>()
        {
            new SubObject("SubItem1", new List<int>() { 1,6,3}),
            new SubObject("SubItem2", new List<int>() { 5,2,9})
        }),
        new MainObject("MainItem2", new List<SubObject>()
        {
            new SubObject("SubItem1", new List<int>() { 0,3,1}),
            new SubObject("SubItem2", new List<int>() { 7,5,2})
        })
    };
}

然后您可以删除构造函数中的显式赋值:

public MainWindow()
{
    InitializeComponent();
    DataContext = dc;
}

通过这些更改,您的 XAML 不再需要提供任何控件名称,您可以直接绑定到相关属性:

<Window x:Class="TestSO42929995WpfNestedData.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestSO42929995WpfNestedData"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <ItemsControl ItemsSource="{Binding VMList}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition/>
              <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding MainObjectName}"/>
            <ItemsControl Grid.Column="1"
                          ItemsSource="{Binding SubObjectData}">
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                  <TextBlock Text="{Binding SubObjectName}"/>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
            </ItemsControl>
          </Grid>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>

上面可能不明显的关键点是每个控件都有一个 DataContext。当您使用 {Binding} 语法时,默认情况下 属性 路径是相对于该上下文的。在顶级控件中,上下文是您在构造函数中设置的内容。但是在单个列表项模板中,上下文是该列表项的单个数据对象,在您的例子中是 MainObject 对象。因此,在这种情况下,您只需绑定到 SubObjectData 属性,就像绑定到 MainObjectName 一样。它的工作原理完全相同,并且出于相同的原因。