使用mvvm

时间:2017-05-11 06:37:30

标签: c# wpf mvvm listbox mvvm-light

我已经问过如何在此线程中将listbox绑定到mvvm: Listbox is not Populating using binding

我创建了另一个线程来询问我的ObservableList为什么不刷新。

列表现已填充在Load

但在选择文件夹

后没有填充或刷新

程序应该在我选择程序后填充列表

这是我的文件夹和类的结构 enter image description here

这是我的FolderBrowserDialogVM的代码

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bates_Writer.ViewModel
{
    public class FolderBrowserDialogVM :ViewModelBase
    {

        public System.Collections.ObjectModel.ObservableCollection<string> FileNames { get; }
    = new System.Collections.ObjectModel.ObservableCollection<string>()
    {
        "Wayne",
        "Cinderella",
        "Malificient"
    };
        public static RelayCommand OpenCommand { get; set; }
        private string _selectedPath;
        public string SelectedPath
        {
            get { return _selectedPath; }
            set
            {
                _selectedPath = value;
                RaisePropertyChanged("SelectedPath");
            }
        }

        private string _defaultPath;

        public FolderBrowserDialogVM()
        {
            RegisterCommands();
        }

        public FolderBrowserDialogVM(string defaultPath)
        {
            _defaultPath = defaultPath;
            RegisterCommands();
        }

        private void RegisterCommands()
        {
            OpenCommand = new RelayCommand(ExecuteOpenFileDialog);
        }

        private void ExecuteOpenFileDialog()
        {
            var dialog = new FolderBrowserDialog();
            dialog.ShowDialog();
            SelectedPath = dialog.SelectedPath;


            FileNames.Clear();

            foreach (var file in System.IO.Directory.EnumerateFiles(SelectedPath, "*", System.IO.SearchOption.AllDirectories))
            {
                FileNames.Add(file);
                Console.WriteLine(file);
            }
        }
    }
}

这是FilesView.xaml(用户控件)的代码

<UserControl x:Class="Bates_Writer.View.FilesView"
             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:Bates_Writer.View"
             xmlns:vm="clr-namespace:Bates_Writer.ViewModel"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <vm:FolderBrowserDialogVM>
        </vm:FolderBrowserDialogVM>
    </UserControl.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding FileNames}" Margin="5,5,5,5"/>
    </Grid>
</UserControl>

我尝试了How to: Implement Property Change Notification

但我无法使其发挥作用。

这是我的FolderBrowserDialogV.xaml(用户控件)

<UserControl x:Class="Bates_Writer.View.FolderBrowserDialogV"
             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:Bates_Writer.View"
             xmlns:vm="clr-namespace:Bates_Writer.ViewModel"
             mc:Ignorable="d" Height="42.056" Width="679.439">
    <UserControl.DataContext>
        <vm:FolderBrowserDialogVM>
        </vm:FolderBrowserDialogVM>
    </UserControl.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="127*"/>
            <ColumnDefinition Width="506*"/>
            <ColumnDefinition Width="110*"/>
        </Grid.ColumnDefinitions>
        <Label Content="Folder Location: " FontSize="14" VerticalContentAlignment="Center"/>
        <TextBox Grid.Column="1" FontSize="14" Margin="5,5,5,5" VerticalContentAlignment="Center" Text="{Binding SelectedPath}"/>
        <Button Command="{Binding OpenCommand}" Content="Browse" Grid.Column="2" FontSize="14" Margin="5,5,5,5" Cursor="Hand"/>

    </Grid>
</UserControl>

我甚至上传了示例项目here

1 个答案:

答案 0 :(得分:1)

看起来您有两个 FolderBrowserDialogVM 实例。一个实例是 FolderBrowserDialogV 的DataContext(这里你绑定了命令,但没有绑定ObservableCollection)和另一个实例 FilesView (这里你绑定了ObservableCollection,但不是命令,因此在此实例中不会更改ObservableCollection)。因为它适用于您的情况,您应该使用(绑定)相同的实例。

从UserControls中删除:

<UserControl.DataContext>
    <vm:FolderBrowserDialogVM>
    </vm:FolderBrowserDialogVM>
</UserControl.DataContext>

在主窗口的资源中创建ViewModel的实例,并将此实例设置为Usercontrols的DataContext:

<MainWindow
xmlns:vm="clr-namespace:Bates_Writer.ViewModel" .. >

<MainWindow.Resources>
<vm:FolderBrowserDialogVM x:Key="vmInstance"/>
</MainWindow.Resources>

<StackPanel>

<YourUserControl1 DataContext="{StaticResource vmInstance}"/>
<YourUserControl2 DataContext="{StaticResource vmInstance}"/>

</StackPanel>

</MainWindow>