【问题标题】:How do I update\refresh the same usercontrol used on multiple views如何更新\刷新多个视图上使用的相同用户控件
【发布时间】:2019-04-28 18:49:52
【问题描述】:

我正在数据库中添加来自视图 2 的记录; 1. 记录插入成功,立即显示在同一视图(View2)的用户控件(DataGrid)中 2. 更改不会显示在视图 1 中,除非我关闭视图或应用程序并重新启动它。

// INotifyProperty 在 ViewModelBase 类中实现

我在两个视图上使用了相同的视图模型。 但我认为它会在每个视图上创建视图模型和数据上下文的新实例。 我想解决这个问题。

---------------查看1------ -------------------

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:UC="clr-namespace:HRSimplified.Controls" 
x:Class="HRSimplified.MainWindow" 
Title="MainWindow" 
Height="628" Width="986">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <UC:EmployeeGridControl Grid.Column="1" />
        </Grid>
</Window>

---------------查看2------ --------------------

<Window
    x:Class="HRSimplified.Windows.EmployeeDashboard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:HRSimplified.View"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:VM="clr-namespace:HRSimplified.ViewModel"
    xmlns:UC="clr-namespace:HRSimplified.Controls"
    mc:Ignorable="d"
    Title="EmployeeDashboard">
    <Window.DataContext>
        <VM:ViewModel_Employee />
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <UC:EmployeeGridControl />
         <ItemsControl Grid.Column="1" ItemsSource="{Binding Path=emp}">
                <StackPanel>                                   
                      <TextBox EditValue="{Binding emp.Name, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}" Margin="5" />
<Button Command="{Binding AddCommand, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, ValidatesOnDataErrors=True}" Margin="5" Height="35" />
                </StackPanel>   
          </ItemsControl>
</Window>

--------------用户控制--------- -------------

<UserControl 
 x:Class="HRSimplified.Controls.EmployeeGridControl"
 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:HRSimplified.View"
 xmlns:system="clr-namespace:System;assembly=mscorlib"
 xmlns:VM="clr-namespace:HRSimplified.ViewModel"
 mc:Ignorable="d" 
 d:DesignHeight="450" 
 d:DesignWidth="800">
<Grid>
<DataGrid x:Name="MasterData" MaxHeight="1080" ItemsSource="{Binding MasterData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}">
            <DataGridTextColumn x:Name="Name" Binding="{Binding Mode=TwoWay}" 
              />           
        </DataGrid>       
    </Grid>
</UserControl>

--------ViewModel_Employe------------------------- --------

public class ViewModel_Employee:ViewModelBase{
        public HRSimplifiedEntities Model = new HRSimplifiedEntities();
        private ObservableCollection<Model.Employee> _MasterData;
        public ObservableCollection<Model.Employee> MasterData
        {
            get
            {
                return _MasterData;
            }
            set
            {
                SetProperty(ref this._MasterData, value);
            }
        }
        private Employee _emp;
        private ICommand _submitCommand;
        public Employee emp
        {
            get { return _emp; }
            set
            {
                _emp = value;
                OnPropertyChanged("EmployeeCollection");
            }
        }
        public ViewModel_Employee()
        {
            MasterData = new ObservableCollection<Model.Employee> 
            (Model.Employees.ToList() as IEnumerable<Employee>);
        }

        public ICommand AddCommand
        {
            get
            {
                if (_submitCommand== null)
                {
                    _submitCommand = new RelayCommand(executeMethod, canExecuteMethod, false);
                }
                return _submitCommand;
            }
        }

        private bool canExecuteMethod(object parameter)
        {
            if (string.IsNullOrEmpty(emp.Name) || string.IsNullOrEmpty(emp.Gender) || 
                string.IsNullOrEmpty(emp.Salary.ToString()))
                return false;
            else
                return true;
        }
        private void executeMethod(object parameter)
        {
                MasterData.Add(emp);

                Model.Employees.Add(emp);
                Model.SaveChanges();

                System.Media.SystemSounds.Beep.Play();
        }}

【问题讨论】:

    标签: c# asp.net .net wpf mvvm


    【解决方案1】:

    我认为您可以创建 ViewModel_Employee 的 Singleton 对象并在所有视图中用作 DataContext。

    单例类

     public class ViewModel_Employee  : INotifyPropertyChanged
        {
    
            private ViewModel_Employee()
            {
    
            }
    
            private static ViewModel_Employee privateInstance = null;
            public static ViewModel_Employee Instance
            {
                get
                {
                    if (privateInstance == null)
                        privateInstance = new ViewModel_Employee();
    
                    return privateInstance;
                }
    
                // class is a singleton
    
            }
    

    所有视图中的数据上下文:

         <Window.DataContext >
                <x:StaticExtension Member="VM:ViewModel_Employee.Instance" />
            </Window.DataContext> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 2012-12-04
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多