这是我的观点主要经过:刷新数据网格添加一个新的客户MVVM WPF
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");
}
}
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
。
如何直接刷新?
你应该学习MVVM。这比所需要的要难得多。 –