我在WPF项目中有一个使用MVVM填充的DataGrid。 当用户选择一行然后单击编辑按钮时,将打开一个窗口。 我的问题是,当用户回到DataGrid时,所选项目已更改并设置为第一行!
我在视图模型中编写了这段代码:
UserInfoDAL userInfoDAL;
public UserInfoListVM()
{
userInfoDAL = new UserInfoDAL();
UserInfoCollection = new ObservableCollection<UserInfo>();
UserInfoModelCurrent = new UserInfo();
UserInfoSelect();
}
private ObservableCollection<UserInfo> _UserInfoCollection;
public ObservableCollection<UserInfo> UserInfoCollection
{
get { return this._UserInfoCollection; }
set
{
this._UserInfoCollection = value;
base.OnPropertyChanged("UserInfoCollection");
}
}
private int _selectedIndex;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value;
base.OnPropertyChanged("SelectedIndex");
}
}
public UserInfo UserInfoModelCurrent
{
get;
set;
}public void UserInfoSelect()
{
SelectedIndex = _selectedIndex;
string _errorMessage = "";
ObservableCollection<UserInfo> _UserInfoModelCollection = new ObservableCollection<UserInfo>();
_UserInfoModelCollection = userInfoDAL.UserInfoSelect(null, out _errorMessage);
if (!string.IsNullOrEmpty(errorMessage))
{
MessageBox.Show(errorMessage, "خطا", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.OK);
}
else
{
UserInfoCollection = _UserInfoModelCollection;
}
Messenger.Default.Send(this, "UserInfoListNotify");
}
xaml中的:
<DataGrid
x:Name="GrdUserInfo"
Grid.Row="1"
ItemsSource="{Binding UserInfoCollection}"
MouseRightButtonDown="GrdUserInfo_MouseRightButtonDown"
LoadingRow="GrdUserInfo_LoadingRow"
AutoGeneratingColumn="GrdUserInfo_AutoGeneratingColumn"
SelectedItem="{Binding UserInfoModelCurrent,Mode=TwoWay}"
Loaded="GrdUserInfo_Loaded"
SelectionChanged="GrdUserInfo_SelectionChanged"
SelectedIndex="{Binding SelectedIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
>
代码隐藏中的:
public UserInfoListView()
{
InitializeComponent();
userInfoListViewModel = new UserInfoListVM();
this.DataContext = userInfoListViewModel;
Messenger.Default.Register<UserInfoListVM>(this, "UserInfoListNotify", x =>
{
DataGridSetting.DataGridPreparation(TabName, ref GrdUserInfo, userInfoListViewModel.UserInfoCollection, SelectedIndexRow);
}
);
}
public bool RestoreGridDefault = false;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.Closing += window_Closing;
window.KeyUp += Window_KeyDown;
}
private void GrdUserInfo_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridSetting.DataGridSetColumnPrperty(ref e);
// e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
}
private void GrdUserInfo_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridSetting.DataGridCreateContexMenu(ref GrdUserInfo, ref RestoreGridDefault, "userInfo", e);
}
private void GrdUserInfo_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = (e.Row.GetIndex()+1).ToString();
}
private void GrdUserInfo_Loaded(object sender, RoutedEventArgs e)
{
DataGridSetting.DataGridPreparation(TabName, ref GrdUserInfo, userInfoListViewModel.UserInfoCollection, SelectedIndexRow);
}
private void GrdUserInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
userInfoListViewModel.SelectedIndex = GrdUserInfo.SelectedIndex;
}
但它没有解决我的问题:(
答案 0 :(得分:0)
令人困惑的一件事是你说你有一个“使用MVVM填充数据的网格”。
MVVM是一个代码设计/架构,其重点不在于使用代码隐藏,但你还是已经完成了。事件处理应该从您的ViewModel完成,而不是代码隐藏。
我建议您阅读MVVM,进一步了解它是如何工作的。
This blog(在我看来)非常了解MVVM基本模式,可以帮助你。