1

コントロールをページのプロパティに正常にバインドしました。バインドしているプロパティはクラスを返します。バインディングは機能しますが、プロパティをクラスの新しいインスタンスに設定した場合、UIは更新されません。

誰かがこの問題を解決するための正しい方向を指している可能性があります。私はINotifyPropertyChangedを実装しようとしましたが、これは機能しません。

コードは次のとおりです。

XAML-非常に基本的なことですが、その瞬間にラベルをプロパティにバインドしようとしています。

<Grid>
        <Label Content="{Binding StudentName}"></Label>
        <Button Width="100" Height="75" Click="Button_Click" ></Button>
</Grid>

C#

this.DataContext = parentWindow.SelectedStudent;

親ウィンドウC#ノート親ウィンドウはINotifyPropertyChangedを実装します

public event PropertyChangedEventHandler PropertyChanged;

public UCStudent SelectedStudent
{
            get
            {
                if (_selectedStudent == null)
                {
                    _selectedStudent = new UCStudent();
                }

                return _selectedStudent;
            }

            set
            {
                if (_selectedStudent != value)
                {
                    _selectedStudent = value;
                    OnPropertyChanged("SelectedStudent");
                }
            }
 }

 protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

問題は、SelectedStudentプロパティを新しく選択された学生に設定すると、バインディングが更新されないことです。

4

1 に答える 1

1

私のアプローチではジェネリックと式を使用しているため、プロパティの変更を厳密に型指定できますが、概念は同じです。私が式を使用する場合、文字列を使用します。PropertyChanged が null の場合は、戻りたいと思うでしょう。それ以外の場合は、最初に新しいイベント ハンドラーを宣言せずにイベントを発生させます。

public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged<T>(Expression<Func<T>> expression)
    {
        if (this.PropertyChanged == null) { return; }

        string propertyName = GetPropertyName(expression);

        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

このアプローチにより、ビュー モデルは通知を厳密に型指定できます。

this.NotifyPropertyChanged(() => this.SelectedApplication);
于 2012-07-02T22:51:10.473 に答える