繁体   English   中英

MVVM ItemsControl简单绑定

[英]MVVM ItemsControl Simple Binding

看起来很简单,但我无法正常工作。 我找不到的任何问题或示例都无法证明我在这里所做的尝试。 我正在尝试将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));
    }
}

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

因此,如下更改视图模型:

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" }
};

暂无
暂无

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

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