C# WPF - Listview绑定无效

3

我一直在尝试在WPF中创建一个窗口,并将listview绑定到一个对象列表,但是我无法在listview中显示列表中的对象。

这是窗口的XAML代码:

<Window x:Class="WpfApplication.window_ManageInstruments"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="window_ManageInstruments" Height="400" Width="600" WindowStartupLocation="CenterOwner">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListView Grid.Row="1" Margin="5" Name="listInstruments" ItemsSource="{Binding InstrumentList}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="120" Header="Symbol" DisplayMemberBinding="{Binding Field1}"/>
                <GridViewColumn Width="200" Header="Description" DisplayMemberBinding="{Binding Field2}"/>
                <GridViewColumn Width="120" Header="Last Stats" DisplayMemberBinding="{Binding Field3}"/>
                <GridViewColumn Width="100" Header="Margin" DisplayMemberBinding="{Binding Field4}"/>
            </GridView>
        </ListView.View>
    </ListView>

    <Label HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" Width="120" Height="25" VerticalAlignment="Top">Symbol</Label>
    <Label HorizontalAlignment="Left" Margin="10,40,0,0" Name="label2" Width="120" Height="25">Description</Label>
    <TextBox Height="25" Margin="150,10,0,0" Name="txtSymbol" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120" />
    <TextBox Margin="150,40,0,0" Name="txtDescription" HorizontalAlignment="Left" Width="120" Height="25" />
    <Button Height="23" HorizontalAlignment="Right" Margin="0,12,133,0" Name="btn_AddInstrument" VerticalAlignment="Top" Width="75">Add</Button>
</Grid>

以下是代码后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using WpfApplication.classes;
using System.Collections.ObjectModel;
namespace WpfApplication { /// /// window_ManageInstruments.xaml 的交互逻辑 /// public partial class window_ManageInstruments : Window { public ObservableCollection InstrumentList = new ObservableCollection();
public window_ManageInstruments() { this.DataContext = this; InstrumentList.Add(new TestClass { Field1 = "A", Field2 = "B", Field3 = "C", Field4 = "D" }); InstrumentList.Add(new TestClass { Field1 = "E", Field2 = "F", Field3 = "G", Field4 = "H" }); InitializeComponent(); } }
public class TestClass { public string Field1 { get; set; } public string Field2 { get; set; } public string Field3 { get; set; } public string Field4 { get; set; } } }

我不确定是否遗漏了任何关键配置,因此非常感谢您的帮助。

提前致谢。

3个回答

5

我认为你的InstrumentList需要成为一个属性。将其改为:

private ObservableCollection _instrumentList = new ObservableCollection();
public ObservableCollection InstrumentList{
  get{ return _instrumentList;}
}

好像这样就行了。很奇怪,因为我之前已经尝试过这个方法,但没有成功。可能是期间做了一些改变,包括看到了LueTm的答案。感谢两位,否则我差点就要开始撞墙了。 - Miguel Mesquita Alfaiate
1
如果您在绑定方面遇到问题,建议在调试器下运行应用程序并查看输出窗口中写入的内容。可能会出现绑定错误,告诉您找不到要绑定的列表。 - Russell Troywest

2
当您将代码更改为以下内容时,它是否有效?
public window_ManageInstruments() 
{
    InitializeComponent();
    InstrumentList.Add(new TestClass { Field1 = "A", Field2 = "B", Field3 = "C", Field4 = "D" });
    InstrumentList.Add(new TestClass { Field1 = "E", Field2 = "F", Field3 = "G", Field4 = "H" });
    this.DataContext = this;
}

2
将InstrumentList的声明更改为属性。
public ObservableCollection<TestClass> InstrumentList { get; set; }

在构造函数中添加以下代码:

InstrumentList = new ObservableCollection<TestClass>();

它应该可以工作,并且集合将会绑定到ListView。

注意 - 我在这里使用了通用集合,您可以根据需要进行更改。

您只能绑定到属性而不是变量。


谢谢您的回答。它实现了Russel的答案相同的结果,但是没有变量和属性之间的分离。然而,我更喜欢使用私有变量和公共getter / setter进行分离。 - Miguel Mesquita Alfaiate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接