0

助けが必要です!

カスタム コントロールを作成し、DependencyProperty次のタイプを追加しました。

Dictionary<string,int>

コントロールが保持されている XAML から、データ バインディングを実行して、コントロールの外側のディクショナリにバインドします。

ここにいくつかのコード スニペットがあります: カスタム コントロールを保持するコントロールのビュー モデル

private Dictionary<string, int> _wordsList;

public Dictionary<string, int> WordsList
{
    get
    {
        return _wordsList;
    }
    set
    {
        _wordsList = value;
        RaisePropertyChanged("WordsList");
    }
}

public WordsViewModel()
{
    //CalculateWordsDictionary returns a dictionary<string,int>
    WordsList = CalculateWordsDictionary(texts);
}

XAML:

<local:MyControl WordsList="{Binding Path=WordsList}" />

カスタム コントロールの背後にあるコード:

public Dictionary<string, int> WordsList
{
    get { return (Dictionary<string, int>)GetValue(WordsListProperty); }
    set { SetValue(WordsListProperty, value); }
}

// Using a DependencyProperty as the backing store for WordsList.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty WordsListProperty =
    DependencyProperty.Register("WordsList", typeof(Dictionary<string, int>), typeof(MyControl), new PropertyMetadata(new Dictionary<string, int>()));

のセットにブレーク ポイントを設定しましたが、DependencyPropertyこのポイントに到達しません。

これが機能しない理由がわかりません...または、辞書をコントロールに渡す他の方法がありますか?

ところで私はMVVM Lightを使用しています

4

2 に答える 2

1

あなたが投稿しなかった重要なもの:)ユーザーコントロール内のバインディングは何ですか? MyControl が依存関係プロパティにバインドされるように、ある種の「ローカル バインディング」を使用する必要があります。次のように ElementName バインディングを使用できます。

編集: MyControl と呼ばれる UserControl のコードは次のとおりです (単なるスニペット)

 <MyControl x:Name=uc>
   <ContentControl Content="{Binding ElementName=uc, Path=WordsList}"/>
于 2013-07-30T06:41:29.493 に答える
0

修理済み!!!!!

すべてのコメントをまとめて質問でした

DataContext が定義されていたので、完全に削除しました。また、プロパティ変更コールバックを登録しました

魅力的に働きました!

ありがとうございます

于 2013-07-30T12:48:36.937 に答える