我在创建“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}">
在我的“多选数据网格”中:
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 本身必须做的事情。
Thanks
奇怪的是,只有我知道的其他地方有Binding
类型属性是DataGridBoundColumn
派生为的类DataGridTextColumn
, DataGridCheckBoxColumn
etc...
有趣的是,该属性不是依赖属性。它是一个普通的 CLR 类型属性。我猜绑定的基础设施是基于不能绑定到绑定类型 DP 的限制。
同一类的其他属性都是非常好的 DP,例如 Visibility、Header 等。
In DataGridBoundColumn
the Binding
属性声明如下,并对相同的内容进行了非常粗略的解释......
这不是 DP,因为如果它获得值就会评估
绑定。
/// <summary>
/// The binding that will be applied to the generated element.
/// </summary>
/// <remarks>
/// This isn't a DP because if it were getting the value would evaluate the binding.
/// </remarks>
public virtual BindingBase Binding
{
get
{
if (!_bindingEnsured)
{
if (!IsReadOnly)
{
DataGridHelper.EnsureTwoWayIfNotOneWay(_binding);
}
_bindingEnsured = true;
}
return _binding;
}
set
{
if (_binding != value)
{
BindingBase oldBinding = _binding;
_binding = value;
CoerceValue(IsReadOnlyProperty);
CoerceValue(SortMemberPathProperty);
_bindingEnsured = false;
OnBindingChanged(oldBinding, _binding);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)