【问题标题】:ListBox not refreshing using mvvmListBox 不使用 mvvm 刷新
【发布时间】:2017-05-11 06:37:29
【问题描述】:

我在这个线程中询问了如何将列表框绑定到 mvvm: Listbox is not Populating using binding

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

列表现在填充在Load

但在我选择文件夹后没有填充或刷新

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

这是我的文件夹和类的结构

这是我的 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

【问题讨论】:

  • SelectedPath.Set 不会自动刷新FileNames。尝试将 FileNames 重构为 property 像 SelectedPath 并在 Set 中添加 RaisePropertyChanged("FileNames");
  • 你的 OpenCommand 绑定在哪里了?
  • @LeiYang 我不明白你的评论。对不起,英语对我来说是一门外语
  • @Rekshino 我更新了我的问题并为我调用 OpenCommand 的 FolderBrowserDialogV.xaml 添加了代码

标签: c# wpf mvvm listbox mvvm-light


【解决方案1】:

您似乎有两个 FolderBrowserDialogVM 实例。一个实例是您的 FolderBrowserDialogV 的 DataContext(在这里您已绑定命令,但未绑定 ObservableCollection),另一个实例是您的 FilesView(此处您已绑定 ObservableCollection,但未绑定命令,所以你的 ObservableCollection 在这种情况下不会改变)。因为它适用于您的情况,您应该使用(绑定到)同一个实例。

从您的用户控件中删除:

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

在主窗口的资源中创建 ViewModel 的实例,并将此实例设置为用户控件的 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>

【讨论】:

  • 如果没有详细说明,例如我需要从用户控件中删除的内容以及需要在主窗口中添加的代码,我将无法理解。非常感谢你,你是生命的救星。我几乎放弃了这个 mvvm 的事情。我一直在使用后面的代码进行编码。我是 wpf 的新手。我从做事中学习,而不是通过阅读,谢谢
猜你喜欢
  • 2012-08-03
  • 2020-11-11
  • 2013-02-24
  • 1970-01-01
  • 2010-10-28
  • 2015-11-06
  • 2015-07-01
  • 2016-03-16
  • 2011-01-18
相关资源
最近更新 更多