我目前正在尝试掌握MVVM的概念。因此,我已经阅读了几个博客并查看了一些项目。但是,我仍然不知道如何应用这个概念。
现在,我正在尝试将String的ObservableCollection绑定到ListBox,以便逐行列出它们的值。我的 ViewModel 目前看起来像这样:
namespace TestApp.ViewModel
{
class StatusViewModel : NotifyPropertyChanged
{
private string _newMsg;
private readonly ObservableCollection<string> _history = new ObservableCollection<string>();
public string Print
{
get { return _newMsg; }
set
{
_newMsg = value;
RaisePropertyChangedEvent("History");
AddToHistory(_newMsg);
}
}
public IEnumerable<string> History
{
get { return _history; }
}
private void AddToHistory(string item)
{
if (!_history.Contains(item))
_history.Add(item);
}
}
}
namespace TestApp.ViewModel
{
public class NotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在View中,我想将ListBox绑定到History
。这是我的XAML:
<UserControl x:Class="TestApp.View.StatusView"
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:vm="clr-namespace:TestApp.ViewModel" mc:Ignorable="d">
<UserControl.Resources>
<vm:StatusView x:Key="statusView"/>
</UserControl.Resources>
<Grid DataContext="{StaticResource statusView}">
<GroupBox Header="StatusView">
<ListBox x:Name="StatusListBox" ItemsSource="{Binding History, Source={StaticResource statusView}}"/>
</GroupBox>
</Grid>
</UserControl>
当我启动TestApp时,我需要访问StatusViewModel
的实例才能将字符串添加到我的ObservableCollection中。我想知道如何做到这一点:
using TestApp.ViewModel;
namespace TestApp.Controller
{
public partial class Startup
{
private void StartTestApp()
{
// from where do I get my 'statObj' in order to do:
statObj.Print = "something";
}
}
}
如何将StatusViewModel对象中的Strings的ObservableCollection绑定到Listbox?
更新
谢谢大家的好评!现在,我了解到我需要保存ViewModel的实例,以便从 View 和 Controller 中访问它。但是,我仍然在努力解决如何以正确的方式做到这一点。## Heading ##
现在,我开始在用户控件的代码隐藏中创建实例。虽然它有效,但我不喜欢我必须在我的视图中引用ViewModel。我想,这是MVVM 不的重点。
此外,我想知道如何在View之外访问我新实例化的StatusViewModel状态,例如在名为 Controller 的其他命名空间中。
using System.Windows.Controls;
using TestApp.ViewModel;
namespace TestApp.View
{
/// <summary>
/// Interaction Logic for StatusView.xaml
/// </summary>
public partial class StatusView : UserControl
{
public StatusView()
{
InitializeComponent();
this.DataContext = new StatusViewModel();
}
public TestApp.ViewModel.StatusViewModel State
{
get { return (DataContext as TestApp.ViewModel.StatusViewModel); }
}
}
}
答案 0 :(得分:0)
将您的XAML更改为:
<UserControl x:Class="TestApp.View.Status"
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:vm="clr-namespace:TestApp.ViewModel" mc:Ignorable="d">
<UserControl.DataContext>
<vm:Status />
</UserControl.DataContext>
<Grid>
<GroupBox Header="Status">
<ListBox x:Name="StatusListBox" ItemsSource="{Binding History}"/>
</GroupBox>
</Grid>
访问您的viemodel的代码应该包含:
Status status = DataContext as Status;
if (status != null)
status.Print = "Hello";
然后它似乎工作:
如果要访问此用户控件的Status实例,请向用户控件添加属性:
public TestApp.ViewModel.Status State // Status gives probably naming conflict
{
get
{
return (DataContext as TestApp.ViewModel.Status);
}
}
答案 1 :(得分:0)
尝试
public ObservableCollection<string> History
{
get { return _history; }
}
private void AddToHistory(string item)
{
if (!History.Contains(item)) History.Add(item);
}