我在创建"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}">
Run Code Online (Sandbox Code Playgroud)
在我的"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); }
}
Run Code Online (Sandbox Code Playgroud)
永远不会调用回调,并且永远不会设置属性.我已经尝试过各种各样的排列,没有回调.唯一让我取得成功的是如果我用字符串替换绑定(例如UriBinding ="hello") - 在这种情况下它会触发回调,并设置属性,但当然会失败,因为它是错误的类型.
我究竟做错了什么?我已经看到了一大堆这样的例子,我想这就是DataGrid必须做的事情.
谢谢