c# - MVVM ItemsControl 简单绑定(bind)

标签 c# wpf xaml mvvm

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

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

最佳答案

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

所以像这样改变你的 View 模型:

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

关于c# - MVVM ItemsControl 简单绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902020/

相关文章:

c# - MySQL 的实体上下文生命周期

xaml - 如何禁用 ListView 中的单击? xamarin.形式

c# - 它的选中状态是怎么去掉的?

c# - 无法将附加属性绑定(bind)到另一个依赖属性

c# - 如何在 wpf 中使用 RelayCommand?

c# - 从 carouselPage 中删除导航栏

c# - Wpf 渐变等效于 css 渐变

c# - 用户名不能包含空值

c# - 从托管代码使用非托管代码

wpf - 为什么 KeyDown 事件会从 TextBox 冒泡出来?