WPF:转到其他页面时,我所有的ListView数据都消失了

时间:2019-02-15 09:17:59

标签: wpf mvvm

我有两页WPF的{​​{1}}应用程序:

Frame

所以我有2个<Window> <Window.DataContext> <common:ViewModel/> </Window.DataContext> <Grid Name="GridMain" Grid.Row="1"> <Frame Name="MyFrame" NavigationUIVisibility="Hidden" Source="Pages/home.xaml"/> </Grid> </Window>

Pages

还有我的Home.xaml Options.xaml

ViewModel

所以在我的public class ViewModelBase : INotifyPropertyChanged { private ObservableCollection<MyData> files; public ICommand SliderSpeedValueChangedCommand { get; set; } public ViewModelBase() { Files = new ObservableCollection<MyData>(); SliderSpeedValueChangedCommand = new SliderSpeedValueChangedCommand(this); } public ObservableCollection<MyData> Files { get { return files; } set { files = value; NotifyPropertyChanged(); } } } 中,我有这个HomePage

ListView

用户选择添加到此<ListView Name="ListViewFiles" ItemsSource="{Binding Files}"/> 中的文件。

现在,当用户切换到另一个ListViewPage),然后又切换回Options.xaml时,此HomePage中的所有文件都消失了。

在这2页内,我也有这个:

ListView

我做错了什么?

更新

这就是我在<Page.DataContext> <viewmodel:ViewModelBase/> </Page.DataContext> <Page.Resources> <vm:ViewModelBase x:Key="ViewModelBase"/> </Page.Resources> 之间切换的方式:

Pages

编辑

所以我有一个简单的命令:

public partial class MainWindow : Window
{
    public ViewModel viewModel;
     private Home home;
     private Options options;

public MainWindow()
{
    InitializeComponent();
    viewModel = new ViewModel();
    home = new Home();
    options = new Options();
}

    private void ListBoxMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int index = ListBoxMenu.SelectedIndex;
        switch (index)
        {
            case 0:
                MyFrame.Content = home;
                break;

            case 1:
                MyFrame.Content = options;
                break;
        }
    }
}

public class AddFilesCommand : ICommand { public event EventHandler CanExecuteChanged; public ViewModelBase ViewModel { get; set; } public AddFilesCommand(ViewModelBase viewModel) { ViewModel = viewModel; } public bool CanExecute(object parameter) { return !ViewModel.AddingFiles; } public void Execute(object parameter) { ViewModel.AddFile(); } } 里面,我正在上这堂课:

ViewModelBase

简单的AddFilesCommand = new AddFilesCommand(this);

Button

然后单击此<Button Command="{Binding AddFilesCommand}"/> 函数Button不会被调用。

1 个答案:

答案 0 :(得分:0)

当您在XAML中编写以下内容时,它会创建ViewModel类的新实例,并将DataContext属性设置为它:

<Window.DataContext>
    <common:ViewModel/>
</Window.DataContext>

由于您是以编程方式创建ViewModel的,因此您应该摆脱这种标记。为每个视图创建一个视图模型的实例,并仅执行一个,例如:

public partial class MainWindow : Window
{
    private readonly Home home;
    private readonly Options options;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
        home = new Home() { DataContext = new ViewModelBase() };
        options = new Options() { DataContext = new ViewModelBase() };
    }

    private void ListBoxMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int index = ListBoxMenu.SelectedIndex;
        switch (index)
        {
            case 0:
                MyFrame.Content = home;
                break;

            case 1:
                MyFrame.Content = options;
                break;
        }
    }
}

并删除这些内容:

<Page.DataContext>
    <viewmodel:ViewModelBase/>
</Page.DataContext>

<Page.Resources>
    <vm:ViewModelBase x:Key="ViewModelBase"/>
</Page.Resources>