1

これが私の状況です。キャンバスに複数のユーザーコントロールがあります。このキャンバスは、XamlWriterを使用してxamlファイルに保存されます。ほとんどの人が知っているように、バインディングはこのメソッドを使用して保存されないため、XamlReaderを使用してユーザーコントロールを読み戻すと、バインディングは存在しなくなります。

簡単なテストとして、XAMLファイルからロードされたComboBox ItemsSourceを再バインドしようとしています(これは、ユーザーコントロール内で問題が発生しているものです)。INotifyPropertyChangedを実装しようとしましたが、変数は次のとおりです。

public event PropertyChangedEventHandler PropertyChanged

ComboItemsPropertyを設定しようとすると、常にnullになります。

public ObservableCollection<string> ComboItemsProperty
{
    get { return ComboItems; } //Field
    set 
    {
        ComboItems = value;
        OnPropertyChanged("ComboItemsProperty");
    }

したがって、私の最終的な目標は、xamlファイルをロードしてから、ComboBoxのItemsSourceにアイテムを追加してから、ComboBoxを新しいアイテムで更新することです。

私はこれを間違った方法で行っていますか?誰かが私にこれの簡単な実用的な例を提供できますか?

編集:

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

XAMLの読み込みと、バインディングが設定されていないことと関係があると確信しています。バインディングを設定してみましたが、うまくいきませんでした。

2番目の編集:

バインディングが原因であると99%確信していると思います。ファイルからコンボボックスをロードしない限り、OnPropertyChangedは正常に機能します。私は次のようにバインディングを設定しようとしました:

Binding bind = new Binding();
bind.Mode = BindingMode.TwoWay;
bind.Source = this.ComboItemsProperty; //Not sure about this line.
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBiding(ComboBox.ItemsSourceProperty, bind);

確認済み。単純なコンボボックスに戻すと、バインドしようとしても機能しません。上記のコードに含まれている必要があります。

4

1 に答える 1

0

bind.SourceComboItemsPropertyプロパティ自体ではなく、を含むオブジェクトを指す必要があります。

Binding bind = new Binding();
bind.Mode = BindingMode.TwoWay;
bind.Source = this;
bind.Path = new PropertyPath("ComboItemsProperty");
this.SetBiding(ComboBox.ItemsSourceProperty, bind);

バインディングが成功したことを確認できます:

 if (GetBindingExpression(ComboBox.ItemsSourceProperty).Status != BindingStatus.Active)
 { 
    //binding didn't work
 }
于 2012-09-05T21:27:40.963 に答える