MVVM ItemsControl简单绑定

时间:2017-10-24 04:06:57

标签: c# wpf xaml mvvm

似乎很简单,但我无法让它发挥作用。我能找到的所有问题或例子都无法证明我在这里尝试的内容。我试图将UserControl中的ItemControl绑定到UserControl的_viewModel,其中名为MyCoolStuff的ObservableCollection包含带有字符串Name的CoolStuff对象。我目前只想在UI中显示Name,并计划从那里开发我的ItemTemplate。如何在不破坏MVVM的情况下设置这些绑定?

XAML:

<UserControl x:Class="MvvmPlayground01.ItemControlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MvvmPlayground01"
             mc:Ignorable="d" 
             Background="Coral"
             d:DesignHeight="300" d:DesignWidth="300">
    <ItemsControl ItemsSource="{Binding MyCoolStuff}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>

代码背后:

public partial class ItemControlView : UserControl
{
    public ItemControlViewModel _viewModel = new ItemControlViewModel();

    public ItemControlView()
    {
        InitializeComponent();
        DataContext = _viewModel;
    }
}

查看型号:

public class ItemControlViewModel : NotificationBase
{
    public ObservableCollection<CoolStuff> MyCoolStuff = new ObservableCollection<CoolStuff>() {
        new CoolStuff() { Name = "Cool Book", Description = "A copy of a really cool book", Date = "1979" },
        new CoolStuff() { Name = "Cool Movie", Description = "A copy of a really cool movie", Date = "1980" },
        new CoolStuff() { Name = "Cool Album", Description = "A copy of a really cool album", Date = "1991" }
    };
}

酷的东西:

public class CoolStuff : NotificationBase
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    private string _description;
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
            OnPropertyChanged("Description");
        }
    }

    private string _date;
    public string Date
    {
        get
        {
            return _date;
        }
        set
        {
            _date = value;
            OnPropertyChanged("Date");
        }
    }
}

通知基础:

public class NotificationBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

1 个答案:

答案 0 :(得分:2)

数据绑定仅适用于公共属性,而不适用于字段。

所以改变你的视图模型:

public class ItemControlViewModel : NotificationBase
{
    public ObservableCollection<CoolStuff> MyCoolStuff { get; set; }

    public ItemControlViewModel()
    {
        MyCoolStuff = new ObservableCollection<CoolStuff>()
        {
            new CoolStuff() { Name = "Cool Book", Description = "A copy of a really cool book", Date = "1979" },
            new CoolStuff() { Name = "Cool Movie", Description = "A copy of a really cool movie", Date = "1980" },
            new CoolStuff() { Name = "Cool Album", Description = "A copy of a really cool album", Date = "1991" }
        };
    }
}

或者这个:

public ObservableCollection<CoolStuff> MyCoolStuff { get; } = new ObservableCollection<CoolStuff>()
{
    new CoolStuff() { Name = "Cool Book", Description = "A copy of a really cool book", Date = "1979" },
    new CoolStuff() { Name = "Cool Movie", Description = "A copy of a really cool movie", Date = "1980" },
    new CoolStuff() { Name = "Cool Album", Description = "A copy of a really cool album", Date = "1991" }
};