Silverlight:在DependencyProperty中包装非DependencyProperty
问题描述:
我希望能够为DataGridTextColumn的某些属性(即宽度,排序顺序等)添加绑定,但是看起来这些属性不是DependencyPropertys,所以他们不能被束缚。另一个答案建议子类DataGridTextColumn作为DependencyPropertys公开这些属性,但我似乎无法找到任何信息如何做到这一点。Silverlight:在DependencyProperty中包装非DependencyProperty
感谢, 罗伯特
答
在Silverlight中,只有FrameworkElement
(不DependencyObject
)子类可以有DependencyProperty
秒。所以不可能直接绑定到DataGridColumn
的属性。
答
试试这个:
public class BindableGridColumn : DataGridTextColumn
{
public DataGridLength BindableWidth
{
get { return Width; }
set {
SetValue(BindableWidthProperty, value);
Width = value;
}
}
// Using a DependencyProperty as the backing store for BindableWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindableWidthProperty =
DependencyProperty.Register("BindableWidth", typeof(DataGridLength), typeof(BindableGridColumn), new PropertyMetadata(DataGridLength.Auto));
}
呃...它不起作用,因为当它通过绑定设置时,不会调用任何回调,并且通过该属性设置时,DependancyProperty不会更改。 – 2009-07-22 21:50:59
对不起,我一定为时过早尝试!是的,我可以看到设置绑定的BindableWidth不会改变DP。在属性设置器中添加了对SetValue的调用。另一种方式是行不通的,即如果直接设置宽度属性,BindableWidth将不会更新,但我认为这不适用于这种情况。 – 2009-07-23 12:22:01
谢谢!但无论如何,因为DataGridColumn不是FrameworkElement,所以它不能被绑定。 – 2009-07-24 21:02:07