2017-06-05 68 views
0

这是我的观点主要经过:刷新数据网格添加一个新的客户MVVM WPF

enter image description here

XAML浏览主营:

<DataGrid AutoGenerateColumns="false" 
      SelectedItem="{Binding Selectedrole}" 
      ItemsSource="{Binding RoleList}"> 
    <Button Content="Add Role" 
      Command="{Binding AddRole}" 
      Height="35"/> 
</DataGrid> 

视图模型主营:

public RoleManagementViewModel() 
{ 
    roleList = new ObservableCollection<UserRoleClass>(WCFclient.GetAllRoles()); 
    _addRole = new RelayCommand<string>(AddRoleFunction);   
}  

private void AddRoleFunction(object obj) 
{ 
    if (!Application.Current.Windows.OfType<SelectionCompartementView>().Any()) 
    {    
     AddRoleView winAddRole = new AddRoleView(); 
     winAddRole.DataContext = new AddRoleViewModel(); 
     winAddRole.Show(); 
     winAddRole.Topmost = true; 
     winAddRole.Focus(); 
    }   
} 

public ObservableCollection<UserRoleClass> RoleList 
{ 
    get { return roleList; } 
    set 
    { 
     roleList = value; 
     OnPropertyChanged("RoleList"); 
    } 
} 

视图添加角色: enter image description here

XAML附加作用:

<Button x:Name="button1" 
     Command="{Binding SaveRole}" 
     CommandParameter="{Binding ElementName=AddRole}"/> 

视图模型附加作用:

public AddRoleViewModel() 
{ 
    _addOrUpdate = new UserRoleClass(); 
    _addOrUpdate = new UserRoleClass(); 
    saveRole = new RelayCommand<Window>(addFunc); 
} 

private void addFunc(Window window) 
{ 
    UserRoleClass newRole = new UserRoleClass() 
    { 
     name = AddOrUpdate.name, 
     description = AddOrUpdate.description, 
    };            
    int resultSave = WCFclient.saveRole(newRole);   
    if (resultSave == 0) 
    { 
     String UpdateInformation0 = "Role is saved successfully"; 
     string sCaption = "Save Role"; 
     MessageBoxButton btnMessageBox = MessageBoxButton.OK; 
     MessageBoxImage icnMessageBox = MessageBoxImage.Information; 
     MessageBoxResult rsltMessageBox = MessageBox.Show(
      UpdateInformation0, sCaption, btnMessageBox, icnMessageBox); 
    } 
    if (window != null) 
    { 
     window.Close(); 
    } 
}  

private ICommand saveRole; 
public ICommand SaveRole 
{ 
    get { return saveRole; } 
} 

它工作正常:当我添加一个新的Role,附加角色的看法关闭并返回到查看Main,并且在数据库中有结果...但不在MainView中的DataGrid

如何直接刷新?

+0

你应该学习MVVM。这比所需要的要难得多。 –

回答

1

首先,你为什么有以下两行? _addOrUpdate = new UserRoleClass();

其次,当您保存新角色时,看起来您正在调用将其保存到数据库的WCF服务。
您正在使用一个可观察的集合,当它添加到它时应该更新,但我没有看到您的代码将新角色添加到RoleList中。

+0

我修好了, 它现在可以工作,非常感谢 – devtunis

+0

@devtunis没有问题。很高兴帮助。为了将来的参考,请记住这个问题,并问问自己:“我在节省什么,我期望发生什么。 (我可能已经做了不止一次) – Noctis

+1

我明白了:) 非常感谢你的建议 我会从现在开始做这个动作 – devtunis

1

最快的方法就是用这样的东西。

public AddRoleViewModel(Action<UserRoleClass> onAdded = null) 
    { 
     _addOrUpdate = new UserRoleClass();   
     _addOrUpdate = new UserRoleClass(); 
     _onAdded = onAdded; 
     saveRole = new RelayCommand<Window>(addFunc);   
    } 

    private void addFunc(Window window) 
    { 
     UserRoleClass newRole = new UserRoleClass() 
     { 
      name = AddOrUpdate.name, 
      description = AddOrUpdate.description, 
     };            
      int resultSave = WCFclient.saveRole(newRole);   
      if (resultSave == 0) 
      { 
       String UpdateInformation0 = "Role is saved successfully"; 
       string sCaption = "Save Role"; 
       MessageBoxButton btnMessageBox = MessageBoxButton.OK; 
       MessageBoxImage icnMessageBox = MessageBoxImage.Information; 
       MessageBoxResult rsltMessageBox = MessageBox.Show(UpdateInformation0, sCaption, btnMessageBox, icnMessageBox); 
      }   

     } 
     _onAdded?.Invoke(newRole);   
     if (window != null) 
     { 

      window.Close(); 
     }  

而且当你创建视图模型

new AddRoleViewModel(newItem=>{ RoleList.Add(newItem); }); 

但我不能说我喜欢的建筑所有的东西。如果可能要查看某种排序messenger service

+0

我尝试了,它也可以工作..谢谢 – devtunis

0

这是将datagrid绑定到可观察集合的常见问题。如果集合更改而非集合内容更改,则绑定更新。要做到这一点,添加新的UserRole到的RoleList后最好的方法是使用一个“深”观察的集合像这样的:

public sealed class DeepObservableCollection<T> 
    : ObservableCollection<T> where T : INotifyPropertyChanged { 

    public event PropertyChangedEventHandler ItemPropertyChanged; 

    public DeepObservableCollection() { 
     CollectionChanged += DeepObservableCollection_CollectionChanged; 
    } 

    public DeepObservableCollection(ObservableCollection<T> collection) 
     : base(collection) { 
     CollectionChanged += DeepObservableCollection_CollectionChanged; 
    } 

    public DeepObservableCollection(List<T> collection) 
     : base(collection) { 
     CollectionChanged += DeepObservableCollection_CollectionChanged; 
    } 

    void DeepObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { 
     if (e.NewItems != null) { 
      foreach (var item in e.NewItems) { 
       var notifyPropertyChanged = item as INotifyPropertyChanged; 
       if (notifyPropertyChanged != null) notifyPropertyChanged.PropertyChanged += item_PropertyChanged; 
      } 
     } 
     if (e.OldItems != null) { 
      foreach (Object item in e.OldItems) { 
       var notifyPropertyChanged = item as INotifyPropertyChanged; 
       if (notifyPropertyChanged != null) notifyPropertyChanged.PropertyChanged -= item_PropertyChanged; 
      } 
     } 
    } 
    void item_PropertyChanged(object sender, PropertyChangedEventArgs e) { 
     NotifyCollectionChangedEventArgs a = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset); 
     OnCollectionChanged(a); 

     ItemPropertyChanged?.Invoke(sender, e); 
    } 
} 

您UserRoleClass必须执行INotifyPropertyChanged(可能使用ObservableObject),但是这一切是什么需要。当您通过绑定添加新的用户角色datagrid更新时。