我需要一些帮助!
我已经构建了一个自定义控件并添加了一个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