你只需要一個DependencyProperty
是你想要它是綁定的,意思是:如果你想有,例如,一個MyBindableProperty
財產在你的控制,與你要能夠做到:
MyBindableProperty={Binding SomeProperty}
但是,如果你想其他DependencyProperties
綁定它,任何財產(無論是DependencyProperty
還是一個正常的)都可以使用。
我不知道你真正需要的,也許你可以澄清更多的,但如果它是你要實現的第一個場景,你可以如下做到這一點:
創建DependencyProperty
,讓我們把它BindableInScopeItemIds
,像這樣:
/// <summary>
/// BindableInScopeItemIds Dependency Property
/// </summary>
public static readonly DependencyProperty BindableInScopeItemIdsProperty =
DependencyProperty.Register("BindableInScopeItemIds", typeof(ICollection<string>), typeof(CustomPivotViewer),
new PropertyMetadata(null,
new PropertyChangedCallback(OnBindableInScopeItemIdsChanged)));
/// <summary>
/// Gets or sets the BindableInScopeItemIds property. This dependency property
/// indicates ....
/// </summary>
public ICollection<string> BindableInScopeItemIds
{
get { return (ICollection<string>)GetValue(BindableInScopeItemIdsProperty); }
set { SetValue(BindableInScopeItemIdsProperty, value); }
}
/// <summary>
/// Handles changes to the BindableInScopeItemIds property.
/// </summary>
private static void OnBindableInScopeItemIdsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = (CustomPivotViewer)d;
ICollection<string> oldBindableInScopeItemIds = (ICollection<string>)e.OldValue;
ICollection<string> newBindableInScopeItemIds = target.BindableInScopeItemIds;
target.OnBindableInScopeItemIdsChanged(oldBindableInScopeItemIds, newBindableInScopeItemIds);
}
/// <summary>
/// Provides derived classes an opportunity to handle changes to the BindableInScopeItemIds property.
/// </summary>
protected virtual void OnBindableInScopeItemIdsChanged(ICollection<string> oldBindableInScopeItemIds, ICollection<string> newBindableInScopeItemIds)
{
}
在OnBindableInScopeItemIdsChanged
,您可以更新內部集合(InScopeItemIds
)
記得要公開財產是只讀(它沒有「二傳手」),所以你可能需要更新它像這樣:
protected virtual void OnBindableInScopeItemIdsChanged(ICollection<string> oldBindableInScopeItemIds, ICollection<string> newBindableInScopeItemIds)
{
InScopeItemIds.Clear();
foreach (var itemId in newBindableInScopeItemIds)
{
InScopeItemIds.Add(itemId);
}
}
希望這有助於:)
控件是開源的嗎? – BenCr 2011-04-05 10:51:51