【问题标题】:WPF listbox binding to List<string> or List<string[]>WPF 列表框绑定到 List<string> 或 List<string[]>
【发布时间】:2014-01-31 01:29:21
【问题描述】:
  • 我对 WPF(尤其是 XAML)非常陌生 *

你好,我的应用程序有一个类,它获取一堆逗号分隔的字符串。所以我做了一个 List 集合来存储字符串。另外,我为列表框制作了 DataTemplate。这是代码。

MainWindow.xaml

...
<DataTemplate x:Key="DataTemplate">
    <Grid>
        <StackPanel Grid.Column="1" Margin="5">
            <StackPanel Orientation="Horizontal" TextBlock.FontWeight="Bold" >
                <TextBlock Text="{Binding AAA}" />
            </StackPanel>
            <TextBlock Text="{Binding BBB}" />
            <TextBlock Text="{Binding CCC}" />
        </StackPanel>
    </Grid>
</DataTemplate>
...
<ListBox x:Name="listbox1" HorizontalAlignment="Left" Height="309" Margin="10,10,0,0" Width="216" BorderThickness="1" VerticalAlignment="Top" ItemTemplate="{DynamicResource HeadlineDataTemplate}"/>

MainWindow.xaml.cs

...
MyClass myClass;

public MainWindow()
{
    InitializeComponent();
    myClass = new MyClass();
}
...
private void Button_Click(object sender, RoutedEventArgs e)
{
    myClass.getData(Textbox1.Text); // I want to convert this to add items to listbox1.
}

MyClass.cs

...
public void getData(string target)
{
    List<string> itemList = new List<string>();
    ...
    while(result != null)
    {
        // This loop gives bunch of comma separated string (more than 100)
        itemList.Add(result);
    }
    // after the loop, itemList has
    // itemList[0] = {"AAA1,BBB1,CCC1"}
    // itemList[1] = {"AAA2,BBB2,CCC2"}
    // itemList[2] = {"AAA3,BBB3,CCC3"}
    // ...
    // I also can store the strings to List<string[]> using string.split().

}

那我该怎么做呢?

我在网上找不到答案。

【问题讨论】:

标签: wpf data-binding collections listbox


【解决方案1】:

我建议创建一个模型类来表示每个 ListBox 项。在此示例中,我将其命名为 MyListBoxItemModel

public class MyListBoxItemModel
{
    public string AAA { get; set; }
    public string BBB { get; set; }
    public string CCC { get; set; }
}

然后在getData函数中,创建MyListBoxItemModel的List作为listbox1的ItemsSource

public void getData(string target)
{
    List<MyListBoxItemModel> itemList = new List<MyListBoxItemModel>();
    ...
    while(result != null)
    {
        var splittedResult = result.Split(',');
        itemList.Add(new MyListBoxItemModel{ 
                                AAA = splittedResult[0], 
                                BBB = splittedResult[1], 
                                CCC = splittedResult[2] 
                            });
    }
    listbox1.ItemsSource = itemList;
}

请注意,此示例仅演示了将数据显示在 ListBox 中所需的最少代码量。不涉及围绕WPF开发的各种技术和最佳实践,如实现INotifyPropertyChanged接口、MVVM模式等。

【讨论】:

  • 我想过这个,但它会让我的应用程序变慢吗?因为它每次都在 while 循环中创建实例。
  • 我不这么认为。因为它只是一个普通的类,它的构造函数中没有逻辑。到目前为止,我看到绑定到模型是更常见的做法,然后绑定到字符串数组
【解决方案2】:

你可以绑定到公共属性,所以如果你写类似的东西

 <TextBlock Text="{Binding BBB}" />

您需要一个具有公共属性“BBB”的对象。

所以除了 har07 的答案之外,您还应该为您的列表使用公共属性(OberservableCollection)

 public OberservableCollection<MyListBoxItemModel> ItemList {get;set;}

那么您对 ​​itemscontrol 的绑定可能如下所示

<ListBox ItemsSource="{Binding ItemList}" ItemTemplate="{DynamicResource HeadlineDataTemplate}"/>

您现在只需要正确的数据上下文;)

【讨论】:

  • 谢谢!使用 ObservableCollection 有什么好处?
  • observablecollection 为 Add() Remove()、Clear() 实现通知 - 这样您就可以在视图中看到集合中的所有更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
相关资源
最近更新 更多