无法显示 List<MyClass>作为我的 ItemsControl 和 StackPanel 视图的列之一? [英] Unable to display a List&lt;MyClass&gt; as one of the columns of my view with ItemsControl and StackPanel?

查看:16
本文介绍了无法显示 List<MyClass>作为我的 ItemsControl 和 StackPanel 视图的列之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class Order
{
    public string OrderName { get; set; }
    public List<Parts> PartsList { get; set; }
}

public class Parts
{
    public string PartName { get; set; }
    public double PartQuantity { get; set; }
}

在我的代码中,我创建了一个 Order 对象列表

In my code I create a list of Order objects

List<Order> myOrders;

我想以某种方式向用户显示所有这些,例如使用元素的堆栈面板,其中第一个是用于显示 OrderName 的 TextBox,第二个是用于显示部件列表的 Datagrid?

I would like to display all of this to the user somehow, like using a stack panel of elements where the first is a TextBox to display OrderName and the second is a Datagrid to display the list of Parts?

老实说,我正在尝试许多不同的东西(我对使用什么类型的控件没有要求),但我永远无法让 PartsList 正确显示(要么我什么也没得到,要么我向用户显示集合".

Honestly I am trying many different things (I have no requirements on what type of controls to use) but I can never get the PartsList to show correctly (either I get nothing OR I get "Collection" show to the user.

目标是看到这样的东西:

The goal would be to see something like this:

Order1    Part1    7
          Part2    12
Order2    Part1    100
          Part2    1
          Part3    58

这是我今天拥有的 XAML,我真的认为它会起作用:

This is the XAML I have today and I really thought it would work:

<ItemsControl x:Name="visual"
              ItemsSource="{Binding myOrders}"
              HorizontalAlignment="Stretch"
              HorizontalContentAlignment="Stretch" 
              Margin="0,397,37,31" Grid.Row="1" Height="172">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <ItemsControl ItemsSource="{Binding }"
                    HorizontalAlignment="Stretch"
                    HorizontalContentAlignment="Stretch">
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <StackPanel>
              <TextBlock Text="{Binding OrderName}"
                         Margin="10" />
              <ItemsControl ItemsSource="{Binding PartsList}"
                            HorizontalAlignment="Stretch"
                            HorizontalContentAlignment="Stretch">
                <ItemsControl.ItemTemplate>
                  <DataTemplate>
                    <Grid HorizontalAlignment="Stretch">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                      </Grid.ColumnDefinitions>
                      <TextBlock Grid.Column="0"
                                 Text="{Binding PartName}"
                                 TextAlignment="Center" />
                      <TextBlock Grid.Column="1"
                                 Text="{Binding PartQuantity}"
                                 TextAlignment="Center" />
                    </Grid>
                  </DataTemplate>
                </ItemsControl.ItemTemplate>
              </ItemsControl>
            </StackPanel>
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

有人看到这段代码有什么问题吗?

Does anyone see what is wrong with this code?

推荐答案

您有一个额外的不必要的 ItemsControl.带有 ItemsSource="{Binding}" 的那个.

You have an extra unnecessary ItemsControl. The one with ItemsSource="{Binding}".

因此假设 myOrders 是一个包含 Order 集合的属性,这应该可以工作:

So assuming that myOrders is a property which holds a collection of Orders this should work:

<ItemsControl x:Name="visual"
              ItemsSource="{Binding myOrders}"
              HorizontalAlignment="Stretch"
              HorizontalContentAlignment="Stretch" Margin="0,397,37,31" 
              Grid.Row="1" Height="172">
  <ItemsControl.ItemTemplate>  
    <!-- This defines the DataTemplate to display one Order object-->   
    <DataTemplate>     
            <StackPanel>
              <TextBlock Text="{Binding OrderName}"
                         Margin="10" />
              <ItemsControl ItemsSource="{Binding PartsList}"
                            HorizontalAlignment="Stretch"
                            HorizontalContentAlignment="Stretch">
                <ItemsControl.ItemTemplate>
             <!-- This defines the DataTemplate to display one Parts object--> 
                  <DataTemplate>
                    <Grid HorizontalAlignment="Stretch">
                      <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                      </Grid.ColumnDefinitions>
                      <TextBlock Grid.Column="0"
                                 Text="{Binding PartName}"
                                 TextAlignment="Center" />
                      <TextBlock Grid.Column="1"
                                 Text="{Binding PartQuantity}"
                                 TextAlignment="Center" />
                    </Grid>
                  </DataTemplate>
                </ItemsControl.ItemTemplate>
              </ItemsControl>
            </StackPanel>        
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于无法显示 List<MyClass>作为我的 ItemsControl 和 StackPanel 视图的列之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆