[英]C# MVVM: Edit a SelectedItem of an ObservableCollection with a RelayCommand
我是編程新手,目前正在學習C#和MVVMC模式(我認為與MVVM模式基本相同)。
我需要為ChiliPlants大學編寫一個數據庫工具。 在那里,您應該能夠從ObservableCollection中編輯現有項。
我在DataGrid中顯示了這個ObservableCollection,請參見: DataGrid在DataGrid下方有三個按鈕:添加,編輯和刪除。 我能夠對AddButton以及DeleteButton進行編程。
不幸的是,我不知道如何對EditButton進行編程。 它應該打開一個新窗口,在其中應按以下方式打開SelectedItem: EditWindow
到目前為止,我的EditButton與AddButton的功能相同。
在這里查看我的代碼:
視圖:
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Button Content="Add" Margin="5,5,0,5" Width="100" Command="{Binding AddCommand}" />
<Button Content="Edit" Margin="5,5,0,5" Width="100" Command="{Binding EditCommand}" />
<Button Content="Delete" Margin="5,5,540,5" Width="100" Command="{Binding DeleteCommand}" />
<Button Content="Sichern" Margin="5,5,5,5" Width="100" Command="{Binding SaveCommand}" />
</StackPanel>
視圖模型:
private ICommand _editCommand;
public ICommand EditCommand
{
get { return _editCommand; }
set { _editCommand = value; }
}
控制器:
public void SDInitialize()
{
var view = new WindowStammdatenverwaltung();
mViewModel = new WindowStammdatenverwaltungViewModel
{
EditCommand = new RelayCommand(EditCommandExecute, EditCommandCanExecute)
};
view.DataContext = mViewModel;
view.ShowDialog();
}
private void EditCommandExecute(object obj)
{
var editedObject = new WindowEditController().EditChiliModel();
if (editedObject != null)
{
mViewModel.Stock.Add(mViewModel.SelectedChili);
}
}
private bool EditCommandCanExecute(object obj)
{
return mViewModel.SelectedChili != null;
}
問題出在EditCommandExecute上。 目前,我剛剛在其中放置了AddCommandExecute的代碼。 不幸的是,我不知道如何編寫這樣的EditCommandExecute。
我的WindowEditController看起來像這樣:
public class WindowEditController
{
WindowEdit mView;
public ChiliModel EditChiliModel()
{
mView = new WindowEdit();
WindowEditViewModel mViewModel = new WindowEditViewModel
{
ChiliModel = new ChiliModel(),
OkCommand = new RelayCommand(ExecuteOkCommand),
CancelCommand = new RelayCommand(ExecuteCancelCommand),
};
mView.DataContext = mViewModel;
if (mView.ShowDialog() == true)
{
return mViewModel.ChiliModel;
}
else
{
return null;
}
}
private void ExecuteOkCommand(object obj)
{
mView.DialogResult = true;
mView.Close();
}
private void ExecuteCancelCommand(object obj)
{
mView.DialogResult = false;
mView.Close();
}
我知道,我可以讓用戶編輯DataGrid內部的SelectedItem,但這在我的任務中是不允許的...
我可以使用與AddCommand相同的窗口嗎? 基本上它們應該看起來一樣,EditWindow應該已經包含了SelectedItem的信息。
我幾乎查找了與該主題相似的所有條目,但沒有找到簡單的解決方案。 或者我可以用我的不良編碼技巧理解的解決方案:( ...
如果你們能幫助我,我將非常高興。 對於這個新手,請保持簡單:)
我已經嘗試過的
我試圖將CommandParameter添加到我的Button中,如下所示: CommandParameter="{Binding SelectedItem, ElementName=StockDataGrid}"
但這仍然沒有打開包含SelectedItem數據的窗口。 它只是為新項目打開了一個全新的窗口。
在Command屬性之后使用CommandParameter
,並將其綁定到DataGrid
的SelectedItem
。
例如,假設您的DataGrid
具有屬性Name=MyDataGrid
。
該Button
變為:
<Button Content="Edit"
Margin="5,5,0,5"
Width="100"
Command="{Binding EditCommand}"
CommandParameter="{Binding SelectedItem, ElementName=MyDataGrid}"/>
當執行EditCommandExecute(object obj)
, obj
實際上是您想要的當前SelectedItem
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.