数据绑定到 UserControl 上的 DependencyObject
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Exposed
依赖项属性声明中,您错误地指定了属性名称“ExposedValue”,而不是“Exposed”。In your
Exposed
dependency property declaration, you have specified the name of the property incorrectly, "ExposedValue", rather than "Exposed".