简体   繁体   English

如何在WPF中将列表列表绑定到列表框?

[英]How to bind a list of lists to a Listbox in WPF?

I have a List of Lists in C# where each list contains 3 integers 我在C#中有一个列表列表,其中每个列表包含3个整数

List<List<int>> Data = new List<List<int>>();
data.Add(new List<int>{ 1, 2, 3});

Now in WPF i have a listbox where i want to bind these values to. 现在在WPF中,我有一个要将这些值绑定到的列表框。 The code below is the code i have now but as expected it says there are no "Item" properties. 下面的代码是我现在拥有的代码,但是正如预期的那样,它说没有“ Item”属性。

<ListBox ItemsSource="{Binding Data}" HorizontalContentAlignment="Stretch">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Item1}" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Item2}" />
        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Item3}" />
      </Grid>
    </DataTemplate>
  </ListBox>

Could someone explain what i have to do in order to get those numbers showing in the listbox? 有人可以解释我要做什么才能使这些数字显示在列表框中?

Thanks in advance. 提前致谢。

You would need to index your bindings within the ListView item template, like so: 您将需要在ListView项目模板中为绑定编制索引,如下所示:

<ListBox ItemsSource="{Binding Data}" HorizontalContentAlignment="Stretch">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding [0]}" />
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding [1]}" />
        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding [2]}" />
      </Grid>
   </DataTemplate>
</ListBox>

However if you plan to have a dynamic number of elements in each List<int> , you could place another ListView within the original ListView . 不过,如果你打算在每个元素的动态数量List<int> ,你可以把其他ListView原来内ListView

Also, this link: https://web.archive.org/web/20120814100526/http://msdn.microsoft.com/en-us/library/ms742451.aspx is really handy if you ever get stuck with binding paths! 另外,此链接: https : //web.archive.org/web/20120814100526/http : //msdn.microsoft.com/en-us/library/ms742451.aspx非常有用,如果您遇到绑定路径问题!

You need to do the TextBlock lines like this: 您需要像这样执行TextBlock行:

<TextBlock Grid.Row="0"
           Grid.Column="0"
           Text="{Binding Path=[0]}" />
<TextBlock Grid.Row="0"
           Grid.Column="1"
           Text="{Binding Path=[1]}" />
<TextBlock Grid.Row="0"
           Grid.Column="2"
           Text="{Binding Path=[2]}" />

If your inner list will ALWAYS have 3 items, I would suggest making a model for binding. 如果您的内部列表始终有3个项目,我建议您为绑定创建模型。 Something like - 就像是 -

    public class TripleIntObject
    {
        public int First { get; set; } 
        public int Second{ get; set; }
        public int Third{ get; set; }
    }

but hopefully your properties are named with more meaning. 但希望您的属性具有更多含义。

So your list changes from List<List<int>> to List<TripleIntObject> 因此,您的列表从List<List<int>>更改为List<TripleIntObject>

Then modify your TextBlocks accordingly - 然后相应地修改您的TextBlocks-

<TextBlock Grid.Row="0"
           Grid.Column="0"
           Text="{Binding First}" />
<TextBlock Grid.Row="0"
           Grid.Column="1"
           Text="{Binding Second}" />
<TextBlock Grid.Row="0"
           Grid.Column="2"
           Text="{Binding Third}" />

If you plan on dynamic items, I would also suggest changing from List<TripleIntObject> to 'ObservableCollection` so that your view will be notified when any data changes. 如果您计划动态项目,我还建议从List<TripleIntObject>更改为'ObservableCollection`,以便在任何数据更改时通知您的视图。 You can read more here . 您可以在这里阅读更多内容。

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

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