2011-04-05 44 views
2

我打算擴展名爲PivotViewer的現有微軟控件。擴展MVVM的現有控件

此控件有一個現有的屬性,我想暴露給我的ViewModel。

public ICollection<string> InScopeItemIds { get; } 

我創建了一個叫做CustomPivotViewer繼承的類,我想創建一個我可以綁定到,將暴露在基類InScopeItemIds舉行的值的依賴項屬性。

我在閱讀DependencyPropertys的同時花了很多時間,並且非常沮喪。

這甚至可能嗎?

+0

控件是開源的嗎? – BenCr 2011-04-05 10:51:51

回答

0

編輯

我意識到誤解,這裏是一個新版本(在原來的問題的情況下):

所以,你可以用你需要綁定的屬性,具有以下請記住以下情況:

  • 由於此屬性是隻讀的,因此您將無法將其用於雙向綁定。
  • 只要包含類型沒有實現INotifyPropertyChanged,用於顯示數據的目標控件將不會收到有關屬性值更改的通知。
  • 只要返回通過該屬性值沒有實現INotifyCollectionChanged(一個例子是的ObservableCollection <Ť>),則變化到集合將不會在其上用於顯示它的目標控制的影響。
+0

問題中的屬性沒有setter。 – BenCr 2011-04-05 10:36:12

+0

這意味着值使用一些你不能改變的類特定的內部代碼進行修改,對吧?在這種情況下,當屬性更改時,您無法在目標控件上看到更改。 – Tengiz 2011-04-05 10:41:26

+0

@Tengiz,提醒我,一種方式綁定設置,並沒有得到,正確的? – BenCr 2011-04-05 10:44:37

0

你只需要一個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); 
    } 
} 

希望這有助於:)

+0

我認爲使用2路綁定已經是他希望綁定屬性的標誌。沒有? – Tengiz 2011-04-05 11:02:45

+0

@Tengiz:不是。你可以有一個'Name'(normal)屬性,它綁定到一個2-way模式的'TextBlock'的'Text'(Dependency)屬性。 ' AbdouMoumen 2011-04-05 13:50:18

+0

你認爲這有意義嗎?任何對此文本塊的更改都不會影響name屬性,所以這個TwoWay的行爲就像OneWay一樣。沒有? – Tengiz 2011-04-05 14:31:24