无法在“MiniListView”类型的“Headers”属性上设置“绑定”。 '绑定'只能在DependencyObject的DependencyProperty上设置

时间:2013-11-30 12:47:54

标签: c# .net wpf wpf-controls

这是我的代码,

    <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绑定到HEADER'S PROPERTY,收到此错误

  

无法在“MiniListView”类型的“标头”属性上设置“绑定”。 '绑定'只能在DependencyProperty上设置   一个DependencyObject。

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

谢谢,

1 个答案:

答案 0 :(得分:0)

依赖项属性注册中的

Owner type不正确。它应该是MiniListView而不是MyListView

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