Wpf 数据绑定到UserControl上的DependencyObject

Wpf 数据绑定到UserControl上的DependencyObject,wpf,data-binding,user-controls,dependency-properties,Wpf,Data Binding,User Controls,Dependency Properties,我有一个UserControl,其布局如下: 绑定到名为Data的ObservableCollection的developerPress网格: public ObservableCollection<SummaryData> Data { get; set; } 以及SummaryData的代码: public class SummaryData : DependencyObject { public string Caption { get; set;

我有一个UserControl,其布局如下:

绑定到名为Data的ObservableCollection的developerPress网格:

 public ObservableCollection<SummaryData> Data { get; set; }
以及SummaryData的代码:

public class SummaryData : DependencyObject
    {
        public string Caption { get; set; }


        public static readonly DependencyProperty ExposedProperty = DependencyProperty.Register("ExposedValue",
                                                                                                             typeof(string),
                                                                                                             typeof(SummaryData),
                                                                                                             new UIPropertyMetadata(null));

        public string Exposed
        {
            get
            {
                return (string)GetValue(ExposedProperty);
            }
            set
            {
                SetValue(ExposedProperty, value);
            }
        }



        public static readonly DependencyProperty UnexposedProperty = DependencyProperty.Register("UnexposedValue",
                                                                                                             typeof(string),
                                                                                                             typeof(SummaryData),
                                                                                                             new UIPropertyMetadata(null));


        public string Unexposed
        {
            get
            {
                return (string)GetValue(UnexposedProperty);
            }
            set
            {
                SetValue(UnexposedProperty, value);
            }
        }

        public string Total
        {
            get
            {
                int exposed;
                int unexposed;
                if (Int32.TryParse(Exposed, out exposed) && Int32.TryParse(Unexposed, out unexposed))
                {
                    return (exposed + unexposed).ToString();
                }
                return String.Format("{0} + {1}", Exposed, Unexposed);
            }
        }
    }
请注意,“暴露”和“未暴露”都是DependencyObject上的DependencyProperties

我的问题是,当我使用网格更新Cases对象时(我知道它确实是通过使用调试器更新的),更改不会反映在标签中。我已经检查过了,输出窗口没有显示数据绑定错误。 此外,如果我恢复为作为常规属性公开,那么初始值的读取就可以了(尽管更新没有反映出来,ofc)

我也尝试过在数据绑定中插入一个转换器(只是一个NOP转换器,它返回给定的任何内容),但它确实从未被调用过


有什么建议吗?:)

Exposed
依赖项属性声明中,您错误地指定了属性名称“ExposedValue”,而不是“Exposed”。

Exposed
依赖项属性声明中,您错误地指定了属性名称“ExposedValue”,而不是“Exposed”.

AFAIK字符串参数只是依赖项属性的自由文本名称,因此我可以随意调用它?无论如何,我已经尝试过改变它,但不幸的是它没有帮助。你有一个完整的例子可供下载,重新创建的问题?通常,对于SummaryData之类的模型类型,您将实现INotifyPropertyChanged,而不是使用依赖属性,因为这将您与WPF联系在一起,并且该模型无法在其他平台上重用。大约一个小时前,我才设法使其正常工作-这完全是个愚蠢的想法,我很尴尬,我在这里张贴开始。。。在UserControl的构造函数中调用InitializeComponent()之后,我正在创建数据绑定到的项,这意味着我正在尝试将数据绑定到null,当然,当我在创建项之后更新值时,数据绑定已经失败了!不过,非常感谢你的帮助!AFAIK字符串参数只是依赖属性的自由文本名称,因此我可以随意调用它?无论如何,我已经尝试过改变它,但不幸的是它没有帮助。你有一个完整的例子可供下载,重新创建的问题?通常,对于SummaryData之类的模型类型,您将实现INotifyPropertyChanged,而不是使用依赖属性,因为这将您与WPF联系在一起,并且该模型无法在其他平台上重用。大约一个小时前,我才设法使其正常工作-这完全是个愚蠢的想法,我很尴尬,我在这里张贴开始。。。在UserControl的构造函数中调用InitializeComponent()之后,我正在创建数据绑定到的项,这意味着我正在尝试将数据绑定到null,当然,当我在创建项之后更新值时,数据绑定已经失败了!不过,非常感谢你的帮助!
        public SummaryData Cases { get; set; }
    private void LoadExampleDataIntoSummaryGrid()
    {
        Cases = new SummaryData() { Caption = "Cases", Exposed = "A", Unexposed = "B" };
        Data.Add(Cases);
        Data.Add(new SummaryData() { Caption = "Controls", Exposed = "C", Unexposed = "D" });
    }
public class SummaryData : DependencyObject
    {
        public string Caption { get; set; }


        public static readonly DependencyProperty ExposedProperty = DependencyProperty.Register("ExposedValue",
                                                                                                             typeof(string),
                                                                                                             typeof(SummaryData),
                                                                                                             new UIPropertyMetadata(null));

        public string Exposed
        {
            get
            {
                return (string)GetValue(ExposedProperty);
            }
            set
            {
                SetValue(ExposedProperty, value);
            }
        }



        public static readonly DependencyProperty UnexposedProperty = DependencyProperty.Register("UnexposedValue",
                                                                                                             typeof(string),
                                                                                                             typeof(SummaryData),
                                                                                                             new UIPropertyMetadata(null));


        public string Unexposed
        {
            get
            {
                return (string)GetValue(UnexposedProperty);
            }
            set
            {
                SetValue(UnexposedProperty, value);
            }
        }

        public string Total
        {
            get
            {
                int exposed;
                int unexposed;
                if (Int32.TryParse(Exposed, out exposed) && Int32.TryParse(Unexposed, out unexposed))
                {
                    return (exposed + unexposed).ToString();
                }
                return String.Format("{0} + {1}", Exposed, Unexposed);
            }
        }
    }