【发布时间】:2011-12-01 17:52:11
【问题描述】:
我在创建“Binding”类型的 DependencyProperty 时遇到问题。其他类型工作正常,如果我使用绑定填充它们,它们会成功解决。
在我的场景中,我想获取原始绑定,以便可以使用它绑定到子对象的属性,这与 DataGrid 处理列的方式非常相似 - 即对于列中指定的每个绑定,它绑定到ItemsSource 集合中的每个项目,而不是绑定 DataContext 本身。
<mg:MultiSelectDataGrid x:Name="Grid" DockPanel.Dock="Left"
ItemsSource="{Binding Path=Rows}" DataContext="{Binding}"
AutoGenerateColumns="False" UriBinding="{Binding Path=UrlItems}">
在我的“MultiSelectDataGrid”中:
public static readonly DependencyProperty UriBindingProperty =
DependencyProperty.Register("UriBinding", typeof(BindingBase),
typeof(MultiSelectDataGrid),
new PropertyMetadata { PropertyChangedCallback = OnBindingChanged});
private static void OnBindingChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
// This is never enterred
}
public BindingBase UriBinding
{
get { return (BindingBase)GetValue(UriBindingProperty); }
set { SetValue(UriBindingProperty, value); }
}
回调永远不会被调用,并且属性永远不会被设置。我已经尝试了各种排列,有回调,没有。唯一让我成功的是,如果我用字符串替换绑定(例如 UriBinding="hello") - 在这种情况下,它会触发回调并设置属性,但当然会失败,因为它是类型错误。
我做错了什么?我已经看过很多这样的例子,我想这就是 DataGrid 自己必须做的事情。
谢谢
【问题讨论】:
标签: c# .net wpf binding dependency-properties