看起来很简单,但我无法让它工作。 我能找到的问题或例子都无法证明我在这里尝试的内容。 我正在尝试将UserControl中的ItemControl绑定到UserControl的_viewModel,其中一个名为MyCoolStuff的ObservableCollection包含带有字符串Name的CoolStuff对象。 我目前只尝试在 UI 中显示名称,并计划从那里开发我的 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" }
};