1

リストにバインドされたデータグリッドがあります。

<DataGrid HorizontalAlignment="Left" SelectedItem="{Binding CurrentPlayer}" Height="374" Margin="121,22,0,0" RowHeaderWidth="0" VerticalAlignment="Top" Width="836" ItemsSource="{Binding Players}" AutoGenerateColumns="false" IsReadOnly="True" SelectionMode="Single" >

ご覧のとおり、項目が選択されると、CurrentPlayer プロパティに保存されます。そのオブジェクトのプロパティは、ユーザーが値を編集できるテキスト ボックスにバインドされます。

私が抱えている問題は、バインディングのために、ユーザーが情報を編集すると (プレイヤー名、アドレスなどを編集する)、ユーザーがまだ [保存] ボタンを押さなくても、変更がすぐにデータグリッドに表示されることです。 .

キャンセルオプションと検証もあるので、明らかにそれは望ましくありません。一度または一方向にバインドできることはわかっていますが、ユーザーが保存ボタンを押すと、変更が表示される必要があります。

これを行う方法はありますか?

4

1 に答える 1

0

考えられる解決策の 1 つ:

ビューモデルの基本クラスがあります。

class BaseClassViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            Debug.Print(info);
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

選択したオブジェクトの各フィールドの各プロパティを、次の形式で get set プロパティにします。

    public string FieldInObject
    {
        get
        {
            return _FieldInObject;
        }
        set
        {
            if (value != _FieldInObject)
            {
                _FieldInObject = value;
                //do not add this code here
                //NotifyPropertyChanged("CurrentPlayer");
            }
        }
    }

ユーザーが保存ボタンを押したときに使用する

NotifyPropertyChanged("CurrentPlayer");
NotifyPropertyChanged("<Property 1 in currentPlayer>"); 
 .....
NotifyPropertyChanged("<Property n in currentPlayer");

CurrentPlayer を更新するように WPF に通知する必要があります。

これが少し理解できることを願っています。NotifyPropertyChanged を使用して、そのプロパティにバインドされているフィールドを更新するように WPF に通知するだけです。

于 2012-11-17T20:43:16.957 に答える