MVVM将ObservableCollection绑定到Listview不起作用

时间:2016-04-14 07:15:02

标签: c# mvvm

我是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一些数据在类人员的循环中具有属性名称和姓氏,我在哪里错了。感谢

2 个答案:

答案 0 :(得分:1)

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>

答案 1 :(得分:0)

您要在视图中绑定的任何属性都应该是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