c# - 如何分配 ItemsSource 属性

标签 c# wpf xaml itemssource

好的,对不起我之前的困惑。

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

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;
    }
}

我还有一个 View 模型,为了简单起见,它使用这两个对象定义了一些数据,如下所示: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 绑定(bind)有效)。因此,剩下要做的就是将 ItemsSource 属性绑定(bind)到 MainObject.SubObjectData 属性。

(我必须添加 Grid.Column 分配,上面的示例中似乎缺少它。)

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

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 不再需要提供任何控件名称,您可以直接绑定(bind)到相关属性:

<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 对象。因此,在该上下文中,您只需绑定(bind)到 SubObjectData 属性,就像绑定(bind)到 MainObjectName 一样。它的工作原理完全相同,原因也相同。

关于c# - 如何分配 ItemsSource 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929995/

相关文章:

c# - WPF 某些图像在加载时旋转

c# - 从 MethodInfo 获取具有私有(private)(不可访问)类型的 Func<>

wpf - WPF 入门

wpf - MVVM 中的模型有什么?

wpf - 如何在 ItemsControls 中使用 AlternationIndex?

wpf - 控制模板可见性触发器

c# - 在 C# 中向虚拟属性添加 setter

c# - 在代码后面设置gridview的AlternatingRowStyle,HeaderStyle,RowStyle

c# - 将通用 COM 对象转换为特定的 .NET 类

xaml - 在 App.xaml 上添加基于样式在 App() { InitializeComponent(); }