我在 WPF C# 中有列表框,其中包含一些条目。在这些条目中,我将只更新单个条目。我想要的是,当我单击“完成编辑”按钮时,我只想阅读更新的(其文本已更改)条目而不是所有其他条目。
我的条目名称是“Harvest_TimeSheetEntry”。我尝试了下面的行,但它读取了所有条目。
Harvest_TimeSheetEntry h = listBox1.SelectedItem as Harvest_TimeSheetEntry;
任何想法?
我在 WPF C# 中有列表框,其中包含一些条目。在这些条目中,我将只更新单个条目。我想要的是,当我单击“完成编辑”按钮时,我只想阅读更新的(其文本已更改)条目而不是所有其他条目。
我的条目名称是“Harvest_TimeSheetEntry”。我尝试了下面的行,但它读取了所有条目。
Harvest_TimeSheetEntry h = listBox1.SelectedItem as Harvest_TimeSheetEntry;
任何想法?
I prefer to address the problem by using the SelectedItem property of a ListBox or CurrentItem of a DataGrid and bind it to a property in my ViewModel.
<ListBox ItemsSource="{Binding HarvestTimeSheet}" SelectedItem={Binding CurrentEntry}.../>
<Button Content="Done Editing..." Command="{Binding DoneEditingCommand}"/>
In my ViewModel I have
private Harvest_TimeSheetEntry _currentHarvest_TimeSheetEntry;
public Harvest_TimeSheetEntry CurrentHarvest_TimeSheetEntry
{
get { return _currentHarvest_TimeSheetEntry; }
set
{
if (_currentHarvest_TimeSheetEntry == value) return;
_currentHarvest_TimeSheetEntry = value;
RaisePropertyChanged("CurrentHarvest_TimeSheetEntry");
}
}
This is set to the selected item in the ListBox.
My ViewModel provides the code for the button. I'm using MVVM light to easily provide the RelayCommand and RaisePropertyChanged.
private RelayCommand _doneEditingCommand;
public RelayCommand DoneEditingCommand
{
get { return _doneEditingCommand ?? (_doneEditingCommand = new RelayCommand(HandleDoneEditing, () => true)); }
set { _doneEditingCommand = value; }
}
public void HandleDoneEditing()
{
if (CurrentHarvest_TimeSheetEntry != null)
//Do whatever you need to do.
}
It's a little more work but you get so much control and flexibility over the flow.