C# WPF - ListView 绑定(bind)不起作用

标签 c# wpf listview binding

我一直在尝试在 WPF 中创建一个窗口并将 ListView 绑定(bind)到对象列表,但我无法让列表中的对象显示在 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 {
    /// 
    /// Interaction logic for 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; }
    }
}

我不知道我是否遗漏了这里的任何关键配置,因此非常感谢任何帮助。

提前谢谢你。

最佳答案

我认为您的 InstrumentList 需要是一个属性。改成

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

关于C# WPF - ListView 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5896198/

相关文章:

wpf - 在代码隐藏中创 build 计数据上下文

android - 使用 SharedPreferences 从 ListView 保存数据

android - 使用 CursorLoader 和 FilterQueryProvider 过滤 ListView?

android - 如何找到 ListView 滚动完成的时间?

c# - 如何在不同的 AppDomain 中运行多个服务实例?

c# - 如何将 targetname 与用户控件和触发器一起使用

c# - 我的 DLL 有 4 个副本而不是 1 个?

c# - 让文本与 TextBlock 的顶部齐平(不是 VerticalAlignment,我保证!)

c# - 当 MVC Core Controller 抛出异常时,如何在中间件中设置不丢弃的 HTTP 响应 header ?

c# - DispatcherTimer 与 Thread.Sleep 的效率对比