0

いくつかのエントリを含む WPF C# のリストボックスがあります。それらのエントリのうち、1 つのエントリのみを更新します。私が欲しいのは、「編集完了」ボタンをクリックすると、他のすべてのエントリではなく、更新された(テキストが変更された)エントリのみを読みたいということです。

私のエントリ名は「Harvest_TimeSheetEntry」です。以下の行を試しましたが、すべてのエントリを読み取ります。

Harvest_TimeSheetEntry h = listBox1.SelectedItem as Harvest_TimeSheetEntry;

何か案が?

4

1 に答える 1

0

ListBox の SelectedItem プロパティまたは DataGrid の CurrentItem プロパティを使用して問題に対処し、それを ViewModel のプロパティにバインドすることを好みます。

<ListBox ItemsSource="{Binding HarvestTimeSheet}" SelectedItem={Binding CurrentEntry}.../>

<Button Content="Done Editing..." Command="{Binding DoneEditingCommand}"/>

私のViewModelには

    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");
        }
    }

これは、ListBox で選択された項目に設定されます。

私のViewModelはボタンのコードを提供します。RelayCommand と RaisePropertyChanged を簡単に提供するために、MVVM ライトを使用しています。

    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.
    }

少し手間がかかりますが、フローを非常に柔軟に制御できます。

于 2013-08-08T11:24:28.440 に答える