【问题标题】:How do I properly set my itemsource in WPF to display my ObservableCollection items?如何在 WPF 中正确设置我的 itemssource 以显示我的 ObservableCollection 项目?
【发布时间】:2018-03-21 16:51:44
【问题描述】:

我有这个项目,我想弄清楚如何将内容添加到列表视图,然后再删除它们。但我似乎无法让 itemsource 绑定,除非我对其进行硬编码。如何在 XAML 中执行此操作?

这会在列表视图中添加一个项目

public partial class MainWindow : Window
    {
        private ObservableCollection<myItem> Item;
        const string pattern = @"((.*)) (.*) left the game";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnAppend_Click(object sender, RoutedEventArgs e)
        {
            Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } };
            lvUsers.ItemsSource = Item;
        }
    }

但是,如果我删除硬编码的 itemsource,即使我将 Itemsource={Binding Item} 添加到 ym XAML,它也不会添加它

<Grid>
        <ListView Name="lvUsers" ItemsSource="{Binding Item}"  HorizontalAlignment="Left" Height="107" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Username}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox Name="tbConent" HorizontalAlignment="Left" Height="78" Margin="10,122,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="497"/>
        <Button Name="btnAppend" Click="btnAppend_Click" Content="Append" HorizontalAlignment="Left" Margin="213,220,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>

编辑

public class myItem
    {
        public string Username { get; set; }
    }

编辑 2

MainWindow.xaml.cs

using System.Collections.ObjectModel;
using System.Windows;

namespace Listviewssssssssssssss
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<myItem> Item { get; private set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnAppend_Click(object sender, RoutedEventArgs e)
        {
            Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } };
            lvUsers.Items.Refresh();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

【问题讨论】:

标签: c# .net xml wpf xaml


【解决方案1】:

Item 设为公共属性:

public ObservableCollection<myItem> Item { get; private set; }

你可以像这样绑定它:

<ListView Name="lvUsers" ItemsSource="{Binding Item, RelativeSource={RelativeSource AncestorType=Window}}" ...>

确保myItem 是公共类型:

public class myItem { ... }

或者您可以将窗口的DataContext 设置为自身:

public partial class MainWindow : Window
{
    public ObservableCollection<myItem> Item { get; private set; }
    const string pattern = @"((.*)) (.*) left the game";

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } };
    }
}

...并直接绑定到源集合:

<ListView Name="lvUsers" ItemsSource="{Binding Item}" ...>

您还应该考虑将属性重命名为“Items”,因为它是一个集合。

编辑:

编辑:如果打算在初始绑定后动态设置Item属性,还需要实现INotifyPropertyChanged接口并引发更改通知:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private ObservableCollection<myItem> _items;
    public ObservableCollection<myItem> Item
    {
        get { return _items; }
        set { _items = value; NotifyPropertyChanged(); }
    }

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnAppend_Click(object sender, RoutedEventArgs e)
    {
        Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } };
    }

    private void btnRemove_Click(object sender, RoutedEventArgs e)
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

【讨论】:

  • 更改 modofer 给了我这种不一致的可访问性:字段类型“ObservableCollection”比字段“MainWindow.Item”更难访问
  • 它仍然没有将用户名添加到列表视图 hm
  • Username 是 myItem 类的公共属性吗?
  • 是的,先生,我会更新问题以便您查看课程
  • 能否请您也发布 MainWindow.xaml.cs 的当前定义?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多