如何在 uwp 中单击按钮将新项目添加到列表视图 [英] how to add new Items to listview on a button click in uwp

查看:33
本文介绍了如何在 uwp 中单击按钮将新项目添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含列表视图和按钮的用户控件.

我想列出要在单击按钮时自动添加到列表视图中的项目.

谢谢

 公共列表AddPicture = new List();公共 EditPhotosUserControl(){this.InitializeComponent();AddPicture.Add(new PhotosGrid { Picture = "/Assets/FamilyOfficeImg/car1.png", placeHolder = "Car Side View", Caption = "Side View" });AddPicture.Add(new PhotosGrid { 图片 = "/Assets/FamilyOfficeImg/interior.png", placeHolder = "汽车内部视图", Caption = "内部视图" });PhotosGridView.ItemsSource = AddPicture;}私有无效 AddButton_Click(对象发送者,RoutedEventArgs e){AddPicture.Add(new PhotosGrid { Picture = "", placeHolder = "Car Interior View", Caption = "Interior View" });PhotosGridView.ItemsSource = AddPicture;}公共类照片网格{公共字符串图片{获取;放;}公共字符串 placeHolder { 获取;放;}公共字符串标题{获取;放;}}

解决方案

首先,您需要更改您的 PhotosGrid 类以实现 INotifyPropertyChanged 以便您不需要重复设置ItemsSource.

下面是我会怎么做.

public class PhotosGrid : INotifyPropertyChanged{私有字符串_图片;公共字符串图片{得到{返回_图片;}设置{_图片=值;OnPropertyChanged("图片");}}私人字符串_placeHolder;公共字符串占位符{得到{返回_placeHolder;}设置 { _placeHolder = 值;OnPropertyChanged("PlaceHolder");}}私人字符串_caption;公共字符串标题{得到 { 返回 _caption;}设置 { _caption = 值;OnPropertyChanged("标题");}}公共事件 PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(字符串名称){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));}}

接下来,您需要将 AddPictureList 更改为

祝你好运.

I have a Usercontrol which contains a listview and a button.

i want to list items to be automatically added in listview when button is clicked.

thank you

 public List<PhotosGrid> AddPicture = new List<PhotosGrid>();
 public EditPhotosUserControl()
        {
            this.InitializeComponent();
            AddPicture.Add(new PhotosGrid { Picture = "/Assets/FamilyOfficeImg/car1.png", placeHolder = "Car Side View", Caption = "Side View" });
            AddPicture.Add(new PhotosGrid { Picture = "/Assets/FamilyOfficeImg/interior.png", placeHolder = "Car Interior View", Caption = "Interior View" });
             PhotosGridView.ItemsSource = AddPicture;


        }

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            AddPicture.Add(new PhotosGrid { Picture = "", placeHolder = "Car Interior View", Caption = "Interior View" });
            PhotosGridView.ItemsSource = AddPicture;


        }
 public class PhotosGrid
    {
        public string Picture { get; set; }
        public string placeHolder { get; set; }
        public string Caption { get; set; }
    }

解决方案

First, you need to change your PhotosGrid Class to Implement INotifyPropertyChanged so that you do not need to repeatedly set ItemsSource.

Below is how I would do it.

public class PhotosGrid : INotifyPropertyChanged
{
    private string _picture;
    public string Picture
    {
        get { return _picture; }
        set { _picture = value; OnPropertyChanged("Picture"); }
    }

    private string _placeHolder;
    public string PlaceHolder
    {
        get { return _placeHolder; }
        set { _placeHolder = value; OnPropertyChanged("PlaceHolder"); }
    }

    private string _caption;
    public string Caption
    {
        get { return _caption; }
        set { _caption = value; OnPropertyChanged("Caption"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

Next, you need to change the AddPicture from List to ObservableCollection so that updating the list will fire INotifyCollectionChanged and INotifyPropertyChanged can be utilized. The official Data Binding in Depth documentation has a table with all the necessary types for C#, C++, and CX.

So your UserControl will be:

public sealed partial class EditPhotosUserControl : UserControl
{
    public ObservableCollection<PhotosGrid> AddPicture = new ObservableCollection<PhotosGrid>();
    public EditPhotosUserControl()
    {
        this.InitializeComponent();
        AddPicture.Add(new PhotosGrid { Picture = "", PlaceHolder = "Car Side View", Caption = "Side View" });
        AddPicture.Add(new PhotosGrid { Picture = "", PlaceHolder = "Car Interior View", Caption = "Interior View" });
        PhotosGridView.ItemsSource = AddPicture;
    }

    private void Button_Tapped(object sender, TappedRoutedEventArgs e)
    {
        AddPicture.Add(new PhotosGrid { Picture = "", PlaceHolder = "Car Interior View", Caption = "Interior View" });
    }
}

This will take care of your i want new item to be added automatically when addButton is clicked

Final Output.

Good Luck.

这篇关于如何在 uwp 中单击按钮将新项目添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆