繁体   English   中英

MVVM将ObservableCollection绑定到Listview不起作用

[英]MVVM binding ObservableCollection to Listview Not working

我是MVVM的新手,我试图将ObservableCollection绑定到Listview。

视图模型

namespace Multiwindow.Viewmodel
{

    public class ViewModelBase
    {
        public Commandclass Cclass { get; set; }

        private ObservableCollection<Person> observableprsn = new ObservableCollection<Person>();

        public ViewModelBase()
        {
            Cclass = new Commandclass(this);
        }

        public void oncommand()
        {   
            for (int i = 0; i < 5; i++)
            {
                Person p = new Person();
                p.Name = "name";
                p.Lastname = "lastname" + i;
                observableprsn.Add(p);
            }           
        }
    }
}

视图

 <Window.Resources>
        <m:Person x:Key="personmodel"/>
        <vm:ViewModelBase x:Key="vmodel"/>
    </Window.Resources>

    <Grid Background="Gray" DataContext="{StaticResource vmodel}">

            <Button Content="Load Window 2" Margin="155,108,177,157"
                    Command="{Binding Cclass, Source={StaticResource vmodel}}" />

            <ListView HorizontalAlignment="Left" Height="100" Width="184" 
                      DataContext="{Binding Source={StaticResource vmodel}}"
                      ItemsSource="{Binding }">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Name}"/>
                        <GridViewColumn Header="Address"  DisplayMemberBinding="{Binding Lastname}"/>
                    </GridView>
                </ListView.View>
            </ListView>

        </Grid>

在按钮上单击,我绑定到listview上具有属性名称和姓氏的类人循环中的一些数据,我在哪里错了。 谢谢

private ObservableCollection<Person> observableprsn = new ObservableCollection<Person>();

而不是私有财产,您需要Public收藏

   public ObservableCollection<Person> Persons
    {
        get { return _observableprsn; }
        set
        {
            if (_observableprsn != value)
            {
                _observableprsn = value;
            }
        }
    }

    private ObservableCollection<Person> _observableprsn { get; set; }

Xaml(这对我有用)

<ListView HorizontalAlignment="Left" Height="100" Width="184" 
                  ItemsSource="{Binding Persons}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Address"  DisplayMemberBinding="{Binding lastName}"/>
            </GridView>
        </ListView.View>
    </ListView>

您希望在视图中绑定的任何属性都应该是DependencyProperty或实现INotifyPropertyChanged 这些属性应该是公共的,以便视图可以访问它们。

像这样实现您的ViewModel:

public class ViewModelBase : INotifyPropertyChanged
{
    private ObservableCollection<Person> _observableprsn = new ObservableCollection<Person>();

    public ObservableCollection<Person> Persons
    {
        get { return _observableprsn; }
        set
        {
            if (_observableprsn != value)
            {
                _observableprsn = value;
                RaisePropertyChanged(() => Persons);
            }
        }
    }

    #region implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocatorAttribute]
    protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        var propertyName = ExtractPropertyName(propertyExpression);
        this.RaisePropertyChanged(propertyName);
    }

    [NotifyPropertyChangedInvocator]
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
    }

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChanged?.Invoke(this, e);
    }

    private static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
    {
        if (propertyExpression == null)
        {
            throw new ArgumentNullException("propertyExpression");
        }

        var memberExpression = propertyExpression.Body as MemberExpression;
        if (memberExpression == null)
        {
            throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
        }

        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
        {
            throw new ArgumentException("The member access expression does not access a property.", "propertyExpression");
        }

        var getMethod = property.GetGetMethod(true);
        if (getMethod.IsStatic)
        {
            throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
        }

        return memberExpression.Member.Name;
    }
    #endregion
}

Person类当然也应该实现DependencyPropertyINotifyPropertyChanged

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM