数据绑定到UserControl上的DependencyObject

时间:2011-10-19 12:59:22

标签: wpf data-binding user-controls dependency-properties

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

绑定到名为Data:

的ObservableCollection的DeveloperExpress网格
 public ObservableCollection<SummaryData> Data { get; set; }

内容绑定到Cases的标签,这是数据集合的第一个元素:

<Label FontStyle="Italic" Margin="2" Content="{Binding ElementName=CaseControlUC, Path=Cases.Exposed, TargetNullValue=IAmNull, Mode=OneWay}" />

其中:

        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" });
    }

而且,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);
            }
        }
    }

请注意,“Exposed”和“Unexposed”都是DependencyObject上的DependencyProperties。

我的问题是,当我使用Grid更新Cases-对象(并且,我知道它确实通过使用调试器更新)时,更改不会反映在标签中。我已经检查过,输出窗口显示没有数据绑定错误。 此外,如果我恢复为Exposed作为常规属性,初始值读取就好了(虽然更新没有反映,ofc。)

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

任何提示都在那里? :)

1 个答案:

答案 0 :(得分:1)

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

相关问题