【问题标题】:C# WPF - Listview Binding not workingC# WPF - Listview 绑定不起作用
【发布时间】:2011-08-19 06:23:38
【问题描述】:

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

这是窗口的 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>

这里是代码隐藏:

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Windows; 使用 System.Windows.Controls; 使用 System.Windows.Data; 使用 System.Windows.Documents; 使用 System.Windows.Input; 使用 System.Windows.Media; 使用 System.Windows.Media.Imaging; 使用 System.Windows.Shapes; 使用 System.Xml; 使用 WpfApplication.classes; 使用 System.Collections.ObjectModel; 命名空间 WpfApplication { /// /// window_ManageInstruments.xaml的交互逻辑 /// 公共部分类window_ManageInstruments:Window { 公共 ObservableCollection InstrumentList = new ObservableCollection(); 公共窗口_管理仪器(){ 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" }); 初始化组件(); } } 公共类TestClass { 公共字符串 Field1 { 获取;放; } 公共字符串 Field2 { 获取;放; } 公共字符串 Field3 { 获取;放; } 公共字符串 Field4 { 获取;放; } } }

我不知道我是否在这里遗漏了任何关键配置,因此我们将不胜感激。

提前谢谢你。

【问题讨论】:

    标签: c# wpf listview binding


    【解决方案1】:

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

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

    【讨论】:

    • 嗯,这似乎可以解决问题。这很奇怪,因为我之前已经尝试过但没有成功。可能我同时做了一些改变,包括 LueTm 的回答。多亏了这两个,我正要开始用头撞墙..
    • 如果您遇到绑定问题,最好在调试器下运行您的应用程序并查看写入输出窗口的内容。可能会出现绑定错误,告诉您找不到要绑定的列表。
    【解决方案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;
    }
    

    【讨论】:

      【解决方案3】:

      将 InstrumentList 声明更改为属性。

      public ObservableCollection<TestClass> InstrumentList { get; set; }
      

      并在构造函数上添加以下行

      InstrumentList = new ObservableCollection<TestClass>();
      

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

      注意 - 我这里使用的是泛型集合,你可以根据需要更改。

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

      【讨论】:

      • 感谢您的回答。它达到了与 Russel 的答案相同的结果,没有变量和属性之间的分离。但是,我确实更喜欢使用私有变量和公共 getter/setter 进行分离。
      猜你喜欢
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多