如何更新\ refresh在多个视图上使用的相同usercontrol [英] How do I update\refresh the same usercontrol used on multiple views

查看:60
本文介绍了如何更新\ refresh在多个视图上使用的相同usercontrol的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中添加了视图2中的记录;

1.记录成功插入并立即显示在同一视图上的usercontrol(DataGrid)中(View2)

2.视图中未显示更改1除非我关闭视图或应用程序并重新启动它。



// INotifyProperty已实施在ViewModelBase类中



View1

 <  窗口  

xmlns = http:// schemas。 microsoft.com/winfx/2006/xaml/presentation\" xmlns:x = HTTP://模式.microsoft.com / winfx / 2006 / xaml

xmlns:UC = clr-namespace:HRSimplified。控件

x:Class = HRSimplified.MainWindow

标题 = MainWindow 高度 = 628 宽度 = 986 >
< Grid >
< 网格.ColumnDefinitions >
< ColumnDefinition 宽度 = * / >
< ColumnDefinition 宽度 = 2 * /
>
< / Grid.ColumnDefinitions >

< UC:EmployeeGridControl 网格.Column = 1 / >
< / Grid >
< ; / Window >



View2

 <   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:转换器 = clr-namespace:HRSimplified.Converters

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 < span class =code-attribute>

< span class =code-attribute> xmlns:UC = clr-namespace:HRSimplified.Controls

mc:可忽略 = d

标题 = EmployeeDashboard >
< Window.DataContext >
< VM:ViewModel_Employee / >
< / Window.DataContext >
< 网格 >
< Grid.ColumnDefinitions >
< ColumnDefinition 宽度 = * / >
< ColumnDefinition 宽度 = 2 * / >
< / Grid.ColumnDefinitions >
< UC:EmployeeGridControl / >
< ItemsControl ItemsSource = {Binding Path = emp} >
< StackPanel >
< TextBox EditValue = {Binding emp.Name,NotifyOnTargetUpdated = True,NotifyOnSourceUpdated = True} 保证金 = 5 / >
< 按钮 命令 = {Binding AddCommand,Up dateSourceTrigger = PropertyChanged,NotifyOnTargetUpdated = True,NotifyOnSourceUpdated = True,ValidatesOnDataErrors = True} 保证金 = 5 高度 = 35 / >
< / StackPanel >
< / ItemsControl >
< / Window >



UserControl

 <   UserControl     

x:Class = HRSimplified.Controls.EmployeeGridControl

xmlns = http://schemas.microsoft .com / winfx / 2006 / xaml / presentation

xmlns:转换器 = clr-namespace:HRSimplified。转换器

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:可忽略 = d

d:DesignHeight = 450

d:DesignWidth = 800 >
< 网格 >
< DataGrid x:名称 = MasterData MaxHeight = 1080 ItemsSource = {Binding MasterData,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged,NotifyOnTargetUpdated = True,NotifyOnSourceUpdated = True} >

< DataGridTextColumn x:名称 = 名称 绑定 = {Binding Mode = TwoWay} / >

< / DataGrid >

< / Grid >
< / UserControl >





ViewModel_Employee



  public   class  ViewModel_Employee:ViewModelBase 
{
public HRSimplifiedEntities Model = new HRSimplifiedEntities();
private ObservableCollection< Model.Employee> _主要的数据;
public ObservableCollection< Model.Employee> MasterData
{
get
{
return _MasterData ;
}
set
{
SetProperty( ref this ._ MasterData, value );
}
}
私人员工_emp;


private ICommand _submitCommand;
public 员工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;
}
}

私人 bool canExecuteMethod( object 参数)
{
if string .IsNullOrEmpty(emp.Name)|| string .IsNullOrEmpty(emp.Gender)||
string .IsNullOrEmpty(emp.Salary.ToString()))
return ;
else
return true < /跨度>;
}
私有 void executeMethod( object 参数)
{
MasterData.Add(emp);

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

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





我的尝试:



我在两个视图上使用相同的视图模型。

但我认为它会在每个视图上创建视图模型和datacontext的新实例。

我想解决这个问题。

解决方案

每个视图都有自己的收藏。



View1 is更新只是因为它正在重新加载整个集合(来自view2的更新)。



View1需要对view2的集合的引用(它可以在其网格中使用)。

I am adding a record from view 2 in the database;
1. The record is inserted successfully and is instantly shown in the usercontrol(DataGrid) on the same view(View2)
2. The changes are not shown in view 1 Unless I Close the the view or application and start it again.

// The INotifyProperty is implemented in ViewModelBase Class

View1

<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="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>

        <UC:EmployeeGridControl Grid.Column="1" />
    </Grid>
</Window>


View2

<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:Converters="clr-namespace:HRSimplified.Converters"

    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 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

<UserControl 

x:Class="HRSimplified.Controls.EmployeeGridControl"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:Converters="clr-namespace:HRSimplified.Converters"

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_Employee

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();
        }



What I have tried:

I used the same view model on both views.
but I think it creates a new instance of the view model and datacontext on each view.
I want to resolve this issue.

解决方案

Each view is getting its own collection.

View1 is updating only because it's reloading the entire collection (with updates from view2).

View1 needs a reference to view2's collection (that it can use in its grid).


这篇关于如何更新\ refresh在多个视图上使用的相同usercontrol的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆