数据绑定到 UserControl 上的 DependencyObject

发布于 2024-12-11 07:43:53 字数 3179 浏览 0 评论 0原文

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

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

 public ObservableCollection<SummaryData> Data { get; set; }

内容绑定到 Cases 的标签,它是 Data 集合的第一个元素:

<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”和“Unexlated”都是 DependencyObject 上的 DependencyProperties。

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

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

有什么提示坑吗? :)

I have a UserControl, that is laid out like the following:

A DeveloperExpress grid bound to an ObservableCollection called Data:

 public ObservableCollection<SummaryData> Data { get; set; }

A label with content bound to Cases, which is the first element of the Data collection:

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

where:

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

And, the code for 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);
            }
        }
    }

Note that "Exposed" and "Unexposed" are both DependencyProperties on a DependencyObject.

My problem is that, when I use the Grid to update the Cases-object (and, I know it is indeed updated by using the debugger) the change is not reflected in the label. I've checked, and the output window shows no databinding-errors.
Furthermore, if I revert to having Exposed as a regular property, the initial value is read just fine (though updates aren't reflected, ofc.)

I've also tried inserting a Converter in the databinding (just a NOP converter that returns whatever it is given), and it is indeed never called...

Any tips pit there? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2024-12-18 07:43:53

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

In your Exposed dependency property declaration, you have specified the name of the property incorrectly, "ExposedValue", rather than "Exposed".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文