【问题标题】:A 'Binding' cannot be set on the 'Headers' property of type 'MiniListView'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject不能在“MiniListView”类型的“标题”属性上设置“绑定”。只能在 DependencyObject 的 DependencyProperty 上设置“绑定”
【发布时间】:2013-11-30 12:47:53
【问题描述】:

这是我的代码,

    <StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,43,43,0">
        <mine:MiniListView x:Name="Mini" Width="300" Headers="{Binding MyHeaders}"></mine:MiniListView>
        <Button HorizontalAlignment="Right" Margin="0" FontSize="10" Content="Add Row" Width="70" Height="20" />
    </StackPanel>

代码背后:

    public Dictionary<string, string> MyHeaders
    {
        get
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("NAME", "BRANCH"); dic.Add("BANKNAME", "Percentage");
            return dic;
        }
    }

我的自定义类:ministView

public class MiniListView : ListView
{
   public static DependencyProperty HeadersProperty;
   public Dictionary<string, string> Headers
   {
      get { return (Dictionary<string, string>)base.GetValue(HeadersProperty); }
      set { base.SetValue(HeadersProperty, value); }
   }
   public MiniListView()
    {
        HeadersProperty = DependencyProperty.Register("Headers", typeof(Dictionary<string, string>), typeof(MyListView));
        this.View = MyGrid();
    }
}

这里我正在尝试用 MyHeaders 绑定 MINlistivew HEADER'S PROPERTY,收到此错误

不能在“MiniListView”类型的“Headers”属性上设置“Binding”。 “绑定”只能在 DependencyProperty 上设置 一个依赖对象。

请告诉我我的代码有什么问题。

谢谢,

【问题讨论】:

    标签: c# .net wpf wpf-controls


    【解决方案1】:

    依赖属性注册中的Owner type 不正确。应该是MiniListView 而不是MyListView

    HeadersProperty = DependencyProperty.Register("Headers",
                       typeof(Dictionary<string, string>), typeof(MiniListView));
    

    【讨论】:

    • 谢谢,但再次引发另一个错误“无法在“DictionaryEntry”类型的“Value”属性上设置“Binding”。只能在 DependencyObject 的 DependencyProperty 上设置“Binding” 。”
    • Value 绑定没有发布问题,这是完全独立的问题,与此无关。错误是不言自明的,您只能绑定到 DP。在Headers 的情况下,您传递了错误的所有者类型,这就是在MiniListView 上找不到DP 的原因。检查Value DP 是否已正确注册。
    • 此外,非常重要的是不是在MiniListView实例构造函数中调用Register,而是在类构造函数中(因为它应该只执行一次)。或者干脆直接在静态成员的声明中:public static readonly DependencyProperty HeadersProperty = DependencyProperty.Register(...);.
    猜你喜欢
    • 2012-07-11
    • 1970-01-01
    • 2021-07-16
    • 2017-10-30
    • 2012-04-19
    • 2019-05-02
    • 1970-01-01
    • 2013-02-21
    • 2011-01-17
    相关资源
    最近更新 更多