页面重新聚焦时重新加载数据

时间:2018-06-07 15:25:54

标签: c# xaml user-controls

我是c#/ xaml / usercontrol中的新手,所以我希望你理解我的问题。

我们的导师禁止我们使用代码,但如果没有它我就看不到解决问题的方法。

问题: 我有几个用户控件,我编辑信息扔掉了dropboxes和普通字段的组合。抛出视图模型(下面添加了示例)。但是,当我更改为不同页面(usecontrol)时,信息会保留在模型中并在模型中进行编辑。

我正在寻找没有运气的方法是在再次加载用户控件时重置后面的数据。

<UserControl x:Class="MyDevices.wpf.UserControls.GroepUserControl"
         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:MyDevices.wpf.UserControls"
         mc:Ignorable="d" 
         DataContext="{Binding GroepVm, Source={StaticResource Locator}}"
         d:DesignHeight="450" d:DesignWidth="800">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="10" />
        <RowDefinition Height="30" />
        <RowDefinition Height="10" />
        <RowDefinition Height="30" />
        <RowDefinition Height="10" />
        <RowDefinition Height="60" />
        <RowDefinition Height="10" />
        <RowDefinition Height="30" />
        <RowDefinition Height="10" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1.2*" />
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="1.8*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="1.2*"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="1.8*"/>
    </Grid.ColumnDefinitions>

    <Label Content="Groep"
           Grid.Row="1"
           Grid.Column="0"
           Style="{StaticResource LabelStyle}"/>
    <ComboBox x:Name="GroepSelected"
              Grid.Row="1" 
              Grid.Column="2"
              ItemsSource="{Binding Groepen, Mode=TwoWay}"
              SelectedItem="{Binding SelectedGroep, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"    
              Style="{StaticResource ComboBoxStyle}"
              IsEnabled="{Binding ElementName=NewGroepCheckbox, Path=IsChecked, Converter={StaticResource BooleanToEnabledConverter}}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Naam}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <!-- Nieuwe Groep -->
    <CheckBox x:Name="NewGroepCheckbox"
              Grid.Row="1"
              Grid.Column="6"
              Content = "Nieuwe Groep"
              IsChecked="{Binding IsCheckBoxChecked, Mode=TwoWay}"
              Style="{DynamicResource CheckBoxStyle}"/>
    <!-- Naam -->
    <Label Content="Naam"
           Grid.Row="3"
           Grid.Column="0"
           Style="{StaticResource LabelStyle}"/>
    <TextBox x:Name="AppNaam"
             Text="{Binding SelectedGroep.Naam, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}"
             Grid.Row="3"
             Grid.Column="2"
             Style="{StaticResource TextBoxStyle}" />
    <!-- Uitleg -->
    <Label Content="Uitleg"
           Grid.Row="5"
           Grid.Column="0"
           VerticalAlignment="Top"
           Style="{StaticResource LabelStyle}"/>
    <TextBox x:Name="AppUitleg"
             Text="{Binding SelectedGroep.Uitleg, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}"
             Grid.Row="5"
             Grid.Column="2"
             Grid.ColumnSpan="5"
             AcceptsReturn="True"
             Style="{StaticResource TextBoxStyle}"/>
    <!-- Cancel button -->
    <Button x:Name="AppCancel"
            Content="Cancel"
            Grid.Row="7"
            Grid.Column="2"
            Command="{Binding CancelCommand}"
            Style="{StaticResource ButtonStyle}"/>
    <!-- Save button -->
    <Button x:Name="AppSave"
            Content="Save"
            Grid.Row="7"
            Grid.Column="6"
            Command="{Binding SaveCommand}"
            Style="{StaticResource ButtonStyle}"/>
</Grid>

using System;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using MyDevices.Domain.Models;
using MyDevices.Domain.Helpers;
using System.Collections.ObjectModel;
using MyDevices.Domain.DataServices;
using System.Windows.Input;

namespace MyDevices.Domain.ViewModels
{
public class GroepViewModel : ViewModelBase, IPage, IDataOperations
{
    private IDataService _dataService = null;
    //private ObservableCollection<ApparaatModel> _apparaten;
    private ObservableCollection<GroepModel> _groepen;
    private GroepModel _selectedGroepModel;
    private GroepModel _newGroep;
    private bool _isCheckBoxChecked;

    public string PageName => "Groep";

    public GroepModel NewGroep
    {
        get => _newGroep ?? (_newGroep = new GroepModel());
        set

        {
            if (_newGroep == value)
                return;

            _newGroep = value;
            RaisePropertyChanged();
        }
    }

    /// <summary>
    /// Geef de lijst terug van alle apparaten in de groep
    /// </summary>
    //public ObservableCollection<ApparaatModel> Apparaten
    //{
    //    get => _apparaten ?? (_apparaten = new ObservableCollection<ApparaatModel>());
    //}

    /// <summary>
    /// Welk groep hebben we geselecteerd
    /// </summary>
    public GroepModel SelectedGroep
    {
        get => _selectedGroepModel ?? (_selectedGroepModel = new GroepModel());
        set
        {
            if (_selectedGroepModel == value)
                return;

            _selectedGroepModel = value;
            RaisePropertyChanged(() => SelectedGroep);
        }
    }

    public ObservableCollection<GroepModel> Groepen
    {
        get => _groepen ?? (_groepen = new ObservableCollection<GroepModel>());
        set
        {
            if (_groepen == value)
                return;

            _groepen = value;
            RaisePropertyChanged();
        }
    }

    private async void GetGroepen()
    {
        Groepen = new ObservableCollection<GroepModel>(await _dataService.GetGroepen());
    }

    public bool IsCheckBoxChecked
    {
        get => _isCheckBoxChecked;
        set
        {
            if (_isCheckBoxChecked == value)
                return;

            _isCheckBoxChecked = value;
            RaisePropertyChanged();
        }
    }

    // Commands
    public ICommand CancelCommand
    {
        get
        {
            return new RelayCommand(() => SelectedGroep = new GroepModel());
        }
    }

    public ICommand SaveCommand => new RelayCommand(Save);

    // Constructor
    public GroepViewModel(IDataService dataService)
    {
        _dataService = dataService;
        _selectedGroepModel = new GroepModel();
        GetGroepen();
        //_apparaten = new List<ApparaatModel>();
    }

    public void Save()
    {
        if (SelectedGroep.GroepId != null)
        {
            NewGroep = SelectedGroep;
            if (IsCheckBoxChecked)
            {
                NewGroep.GroepId = new Guid();
            }
            _dataService.AddGroep(NewGroep);
            NewGroep = new GroepModel();
            GetGroepen();
        }
    }

    public void Reload()
    {
        GetGroepen();
    }
}

}

0 个答案:

没有答案