【问题标题】:windows store dynamic data binding lists and other typeswindows存储动态数据绑定列表等类型
【发布时间】:2017-09-11 17:27:34
【问题描述】:

我希望这里的任何人都可以帮助我动态绑定数据。当我在按钮上添加列表项时,它不会在我的页面中更新时,这也可以为int添加一个值。然而,当我输入文本框时,int会改变...所以我不明白发生了什么问题。我也不知道这是否是正确的方法。

因此快速概述我的Qeukions:

  1. 为什么这不起作用?
  2. 这是这样做的正确方法吗?
  3. 有人可以告诉我如何使它工作?

假定的错误位置:MainViewModel类中的NotifyPropertyNged方法。 这是我项目中的所有代码,刚刚在另一个地方出现问题而不是我想的。

我的xaml页面:

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:databinding_unittest0"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModels="using:databinding_unittest0.ViewModels"
x:Class="databinding_unittest0.MainPage"
mc:Ignorable="d">

<Page.DataContext>
    <ViewModels:MainViewModel/>
</Page.DataContext>

<Grid>
    <TextBox Text="{Binding Testvar, Mode=TwoWay}" Height="100" Margin="110,10,1023,658"/>
    <TextBlock Text="{Binding Testvar}" Height="100" Margin="110,115,1023,553"/>
    <ListBox ItemsSource="{Binding Persons}" Height="500" VerticalAlignment="Bottom">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem Content="{Binding Fullname}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Button Content="Add 1" HorizontalAlignment="Left" Margin="441,46,0,0" VerticalAlignment="Top" Click="Add1_Click"/>
    <Button Content="Add list item" HorizontalAlignment="Left" Margin="609,46,0,0" VerticalAlignment="Top" Click="AddListItem_Click"/>
</Grid>
</Page>

页面的C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using databinding_unittest0.ViewModels;
using databinding_unittest0.Models;

// The Blank Page item template is documented at 
// http://go.microsoft.com/fwlink/?LinkId=234238

namespace databinding_unittest0
{

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainViewModel mvm { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
            mvm = new MainViewModel();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Add1_Click(object sender, RoutedEventArgs e)
        {
            mvm.Testvar = mvm.Testvar + 1;
        }

        private void AddListItem_Click(object sender, RoutedEventArgs e)
        {
            mvm.Persons.Add(new Person() { Firstname="Kurt", Lastname="Cobain"});
        }
    }
}

mainviewmodel c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel;

using databinding_unittest0.Models;


namespace databinding_unittest0.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private List<Person> persons;

        public List<Person> Persons
        {
            get { return persons; }
            set
            {
                persons = value;
                NotifyPropertyChanged();
             }
        }

        private int testvar;

        public int Testvar
        {
            get { return testvar; }
            set
            {
                testvar = value;
                NotifyPropertyChanged();
             }
        }

        public MainViewModel()
        {
            Persons = new List<Person>();
            Persons.Add(new Person() { Firstname = "Henk", Lastname = "Jansen" });
        }


        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (propertyName != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

人c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace databinding_unittest0.Models
{
    public class Person
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Fullname
        {
            get
            {
                return Firstname + " " + Lastname;
            }
        }
    }
}

【问题讨论】:

  • 尝试使用devalipablecollection 而是列表。另外 - 单击按钮事件不能在视图后面的代码中。尝试绑定事件。 (这是mvvm) span>
  • 我怎么样绑定呢?还要感谢分配 span>
  • 看看这里:) channel9.msdn.com/Series/… span>
  • aaa,你是含义,如何绑定事件?看看这里:github.com/gtteamamxx/ZS1Planv2/blob/master/ZS1Planv2/View/…你有很多框架使用mvvm。我用了交互性。 (也检查此页面的视图)。 abberalablecollection修复了您的问题? span>

标签: c# xaml windows-store-apps


【解决方案1】:

在视图模型中,有两种基本方法可以让绑定目标(xaml 控件)知道绑定源(您的属性)发生了有趣的事情。第一个是通过视图模型的 INotifyProperty 实现更改,您的视图模型通过为 Persons 属性提供属性更改通知来正确实现它。

但是简单地向列表中添加一个成员并不构成对 Persons 属性本身的更改;只有当您通过属性设置器更改该属性时才会发生这种情况,而 List.Add 不会这样做。

集合需要一种方法来通知绑定目标集合中的某些内容已更改,而这由集合本身通过实现 INotifyCollectionChanged 接口来完成,而 List 没有这样做。幸运的是,框架提供了 ObservableCollection,它确实实现了这些接口,所以只需将 List 更改为 ObservableCollection 就可以了。

请参阅this question 以获得有关 ObservableCollectionT 的大量答案>。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多