使用MVVM的Wpf应用程序 [英] Wpf application with MVVM
问题描述
大家晚上好,我希望开发一个简单的应用程序来理解使用WPF的MVVM架构。我只想在按下添加按钮时插入在列表视图中输入的文本字段中输入的文本。
我创建了两个文件夹,它们是Template和ViewModel,这里是每个文件知道的源代码插入我使用MVVMLightToolkit扩展。
我不知道为什么它会在listviewArticleManagement.Model.Article的第一行显示我?我该如何解决这个问题呢?
我的尝试:
表示模板文件夹:
Article.cs
Good evening everyone, I am looking to develop a simple application to understand MVVM architecture with WPF. I just want to insert a text entered in a text field entered on a listview when pressing the add button.
I create two folders that are Template and ViewModel and here is the source code insert for each file knowing that I use the MVVMLightToolkit extension.
I do not find why it shows me in the first line of listviewArticleManagement.Model.Article? and how can I fix this problem?
What I have tried:
for the template folder:
Article.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GestionArticle.Model
{
public class Article
{
private string _nom;
public string Nom
{
get { return _nom; }
set { _nom = value; }
}
}
}
和ViewModel文件夹:
IMainViewModel.cs
and for the ViewModel folder:
IMainViewModel.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GestionArticle.Model;
namespace GestionArticle.ViewModel
{
public interface IMainViewModel
{
string Titre { get; set; }
ObservableCollection<Article> Articles { get; }
ICommand ChargerArticleCommand { get; }
}
}
MainViewModel.cs
MainViewModel.cs
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using GestionArticle.Model;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
namespace GestionArticle.ViewModel
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase, IMainViewModel
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
private readonly ObservableCollection<Article> article;
public MainViewModel()
{
article = new ObservableCollection<Article>();
article.Add(new Article { Nom = "article 1" });
ChargerArticleCommand = new RelayCommand(ChargerArticles);
}
private void ChargerArticles()
{
this.article.Add(new Article { Nom = "Article 2" });
}
private string _titre;
public string Titre
{
get { return _titre; }
set
{
_titre = value;
RaisePropertyChanged("Titre");
}
}
public ObservableCollection<Article> Articles
{
get { return this.article; }
}
public ICommand ChargerArticleCommand
{
get;
private set;
}
}
}
ViewModelLocator.cs
ViewModelLocator.cs
/*
In App.xaml:
<Application.Resources>
<vm:ViewModelLocator xmlns:vm="clr-namespace:GestionArticle.ViewModel"
x:Key="Locator" />
</Application.Resources>
In the View:
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
namespace GestionArticle.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class ViewModelLocator
{
static ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();
}
public static IMainViewModel MainVM
{
get { return ServiceLocator.Current.GetInstance<IMainViewModel>(); }
}
/// <summary>
/// Gets the Main property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
/* public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}*/
public static void CleanMain()
{
SimpleIoc.Default.Unregister<IMainViewModel>();
SimpleIoc.Default.Register<IMainViewModel, MainViewModel>();
}
public static void Cleanup()
{
CleanMain();
}
}
}
App.xaml
App.xaml
<Application x:Class="GestionArticle.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="clr-namespace:GestionArticle.ViewModel"
StartupUri="MainWindow.xaml"
mc:Ignorable="d">
<Application.Resources>
<!--Global View Model Locator-->
<viewModel:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
</Application.Resources>
</Application>
MainWindow.xaml
MainWindow.xaml
<Window x:Class="GestionArticle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
Height="410"
Width="613"
Title="MVVM Light Application"
DataContext="{Binding MainVM, Source={StaticResource Locator}}">
<!-- <Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>-->
<Grid x:Name="LayoutRoot">
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="#FFF3ACF3"
Text="{Binding WelcomeTitle}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap" Margin="317.5,138,76.5,194" Width="211" Background="#FF7FE6FD" />
<ListView HorizontalAlignment="Left" Height="225" Margin="59,31,0,0" VerticalAlignment="Top" Width="185" ItemsSource="{Binding Articles, Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumn/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Ajouter" HorizontalAlignment="Left" Height="40" Margin="308,292,0,0" VerticalAlignment="Top" Width="162" Command="{Binding ChargerArticleCommand, Mode=OneWay}"/>
</Grid>
</Window>
我跑步时默认情况下找不到文章对象:
图片
推荐答案
看起来像文章模型是添加到ListView但是您没有正确设置列。看看这篇文章: [ ^ ]
It looks like the Article model is added to the ListView however you have not set up your column correctly. Check out this article: [^]
这篇关于使用MVVM的Wpf应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!