【问题标题】:databinding dictionary <string,int> to a dependency property数据绑定字典 <string,int> 到依赖属性
【发布时间】:2013-07-30 06:31:12
【问题描述】:

我需要帮助!

我已经构建了一个自定义控件并添加了一个DependencyProperty 类型:

Dictionary<string,int>

并从控件所在的 XAML 中,我执行数据绑定以绑定到控件外部的字典。

这里有一些代码 sn-ps: 持有自定义控件的控件的视图模型

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

【问题讨论】:

  • 能否为 DependencyProperty 添加 Propertychanged 回调并检查,public static DependencyProperty FirstProperty = DependencyProperty.Register("First", typeof(string), typeof(MyType), new FrameworkPropertyMetadata( false, new PropertyChangedCallback(OnFirstPropertyChanged) )));私有静态无效 OnFirstPropertyChanged(DependencyObject 发送者,DependencyPropertyChangedEventArgs e){ PropertyChangedEventHandler h = PropertyChanged; if (h != null) { h(sender, new PropertyChangedEventArgs("Second")); } }
  • 如果我理解正确,我应该尝试在控件后面的代码中捕获属性更改事件...我试图这样做:(stackoverflow.com/questions/12798909/…)没有运气
  • 那么你的绑定应该有问题。定义转换器并在转换器中放置断点,并确保您的绑定没有任何问题。您还可以检查输出窗口是否存在绑定错误
  • 当在 XAML 中和/或通过绑定设置属性时,不会调用 WordsList 设置器。有关说明,请参阅here。您必须通过属性元数据注册PropertyChangedCallback
  • MyControlDataContext 是否设置在任何地方?

标签: c# wpf data-binding mvvm dependency-properties


【解决方案1】:

您没有发布的重要内容:) 您在用户控件中的绑定是什么?您必须使用某种“本地绑定”,以便您的 MyControl 绑定到它的依赖属性。您可以像这样使用 ElementName 绑定:

编辑:这里是名为 MyControl 的 UserControl 的代码(只是一个 sn-p)

 <MyControl x:Name=uc>
   <ContentControl Content="{Binding ElementName=uc, Path=WordsList}"/>

【讨论】:

  • 我发布的是自定义控件在它所在的用户控件的 xaml 中的实际定义。“本地”只是自定义控件的命名空间,因此我可以通过 xaml 访问它.我现在更改了名称,以便更清楚:
  • 你没看懂我,重要的代码是你的usercontrol的xaml定义。这就是我的 sn-p 的用途。因为我不知道 Wordslist 在您的用户控件中绑定到哪里,所以我将它绑定到 ContentControl。您与用户控件的绑定是正确的,但这不是问题。至少你应该告诉我们你想用你的依赖属性实现什么
  • 我有一个用户控件,它在他的 xaml 中保存了一个自定义控件。我想构建一个自定义标签云控件,该控件通过依赖属性a Dictionary (单词及其出现次数)。所以我真的不明白你建议解决这个问题。
  • 然后只需将我的 contentcontrol 更改为您的 Cloudcontrol。但是如果你在用户控件中使用依赖属性并且你想在你的用户控件中绑定它,那么你必须使用 ElementName Binding 或类似的东西。这里有更多信息stackoverflow.com/questions/12404337/…stackoverflow.com/questions/11226843/…
【解决方案2】:

已修复!!!!!!

所有的cmets加在一起就是问题

我定义了一个 DataContext,我完全删除了它。我还注册了属性更改回调

发挥了魅力!

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2013-02-10
    • 2023-03-26
    • 2018-05-21
    • 1970-01-01
    • 2010-10-09
    相关资源
    最近更新 更多