这是我的观点主要:
XAML查看主要内容:
<DataGrid AutoGenerateColumns="false"
SelectedItem="{Binding Selectedrole}"
ItemsSource="{Binding RoleList}">
<Button Content="Add Role"
Command="{Binding AddRole}"
Height="35"/>
</DataGrid>
ViewModel Main:
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");
}
}
Xaml Add-Role:
<Button x:Name="button1"
Command="{Binding SaveRole}"
CommandParameter="{Binding ElementName=AddRole}"/>
ViewModel添加角色:
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
时,Add-Role的视图关闭并返回到主视图,我在数据库中有结果...但不在{{1}中在MainView中。
如何直接刷新?
答案 0 :(得分:1)
首先,为什么你有以下两行?
_addOrUpdate = new UserRoleClass();
其次,当您保存新角色时,您似乎正在调用将其保存到数据库的WCF服务。
您使用了一个可观察的集合,该集合应该在您添加时更新,但我没有看到您的代码将新角色添加到RoleList
。
答案 1 :(得分: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();
}
当您创建ViewModel
时 new AddRoleViewModel(newItem=>{ RoleList.Add(newItem); });
但我不能说我喜欢这个架构。如果可能想要查看某种messenger service
答案 2 :(得分: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更新时。