Je voudrais demander comment remplir la liste en utilisant le modèle MVVM Je suis débutant dans le modèle mvvm, j'apprends plus de choses à faire que de lire. Je l'ai fait avant d'utiliser wpf mais j'utilise le code derrière.Populate Listview en utilisant mvvm
J'utilise Mvvm Light. Ce que je veux parcourir l'emplacement du dossier, puis remplir le listview avec les fichiers à l'intérieur
Jusqu'à présent, je l'ai déjà un dossier Parcourir
j'ai ce code
public class OpenFileDialogVM : ViewModelBase
{
public static RelayCommand OpenCommand { get; set; }
private string _selectedPath;
public string SelectedPath
{
get { return _selectedPath; }
set
{
_selectedPath = value;
RaisePropertyChanged("SelectedPath");
}
}
private string _defaultPath;
public OpenFileDialogVM()
{
RegisterCommands();
}
public OpenFileDialogVM(string defaultPath)
{
_defaultPath = defaultPath;
RegisterCommands();
}
private void RegisterCommands()
{
OpenCommand = new RelayCommand(ExecuteOpenFileDialog);
}
private void ExecuteOpenFileDialog()
{
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
SelectedPath = dialog.SelectedPath;
}
}
et j'ai ce code pour le contrôle utilisateur
<UserControl x:Class="MvvmLight1.FolderDialog"
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:MvvmLight1"
xmlns:local="clr-namespace:MvvmLight1"
mc:Ignorable="d" d:DesignWidth="300" Height="186.916" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="90*"/>
<RowDefinition Height="97*"/>
</Grid.RowDefinitions>
<Grid>
<TextBox Text="{Binding SelectedPath}" />
</Grid>
<Grid Grid.Row="1" >
<Button Command="vm:OpenFileDialogVM.OpenCommand" >Browse</Button>
</Grid>
</Grid>
</UserControl>
jusqu'à présent, la navigation fonctionne. Ma question est comment puis-je appeler ce code. après la sélection du dossier afin que je puisse remplir ma listview?
private void Call(string selectedpath)
{
try
{
var allFiles = Directory.GetFiles(selectedpath, "*", SearchOption.AllDirectories);
foreach (var item in allFiles)
{
System.Console.WriteLine(item);
//code for populating listview
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.StackTrace);
throw ex;
}
}
Merci pour le temps.
Note: ajouter une référence pour System.Windows.Forms –
Oui. pouvez-vous m'aider ou me donner un extrait de code comment appeler le code en utilisant MVVM? Après avoir sélectionné un dossier Merci – classname13
Reproduire dans mon IDE maintenant. Pour référence future, voir [MCVE] (https://stackoverflow.com/help/mcve) sur la façon d'écrire une question (c'est un très bon travail cependant). –