2010-06-07 89 views
4

我有一個複雜的值對象類,它具有1)數字或只讀屬性; 2)私人構造函數; 3)許多靜態單例實例屬性[因此ComplexValueObject的屬性永不改變,並且在應用程序的生命週期中一次實例化一個單獨的值]。XAML綁定到複雜值對象

public class ComplexValueClass 
{ 
    /* A number of read only properties */ 
    private readonly string _propertyOne; 
    public string PropertyOne 
    { 
     get 
     { 
      return _propertyOne; 
     } 
    } 

    private readonly string _propertyTwo; 
    public string PropertyTwo 
    { 
     get 
     { 
      return _propertyTwo; 
     } 
    } 

    /* a private constructor */ 
    private ComplexValueClass(string propertyOne, string propertyTwo) 
    { 
     _propertyOne = propertyOne; 
     _propertyTwo = PropertyTwo; 
    } 

    /* a number of singleton instances */ 
    private static ComplexValueClass _complexValueObjectOne; 
    public static ComplexValueClass ComplexValueObjectOne 
    { 
     get 
     { 
      if (_complexValueObjectOne == null) 
      { 
       _complexValueObjectOne = new ComplexValueClass("string one", "string two"); 
      } 
      return _complexValueObjectOne; 
     } 
    } 

    private static ComplexValueClass _complexValueObjectTwo; 
    public static ComplexValueClass ComplexValueObjectTwo 
    { 
     get 
     { 
      if (_complexValueObjectTwo == null) 
      { 
       _complexValueObjectTwo = new ComplexValueClass("string three", "string four"); 
      } 
      return _complexValueObjectTwo; 
     } 
    } 
} 

我有一個數據上下文類,它看起來是這樣的:

public class DataContextClass : INotifyPropertyChanged 
{ 


    private ComplexValueClass _complexValueClass; 
    public ComplexValueClass ComplexValueObject 
    { 
     get 
     { 
      return _complexValueClass; 
     } 
     set 
     { 
      _complexValueClass = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("ComplexValueObject")); 
     } 
    } 
} 

我想編寫一個XAML約束力的聲明的屬性我複雜的值對象上更新UI每當整複雜的價值對象變化。什麼是最好的和/或最簡潔的做法?我有類似:

<Object Value="{Binding ComplexValueObject.PropertyOne}" /> 

但用戶界面不會更新時ComplexValueObject作爲一個整體的變化。

回答

3

您的原始方案應該工作得很好,因爲在大多數情況下,Bindings可以識別其屬性路徑任何部分的更改通知。實際上,我嘗試了您發佈的代碼進行確認,並確實可以正常工作。

您的剝離樣本中是否可能沒有其他複雜性?主要的我可以想到的是collections-> ItemsSource綁定,但可能有一些與您將屬性分配給綁定值的屬性有關(因爲它顯然不是對象)或其他東西。

1

您不通知PropertyOne的更改,以便UI不會更新。而是綁定到ComplexValueObject並使用值轉換器來獲取屬性值。

<Object Value="{Binding Path=ComplexValueObject, Converter={StaticResource ComplexValueConverter}, ConverterParameter=PropertyOne}" /> 

public class ComplexValueConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
    ComplexValue cv = value as ComplexValue; 
    string propName = parameter as string; 
    switch (propName) 
    { 
     case "PropertyOne": 
     return cv.PropertyOne; 
     case "PropertyTwo": 
     return cv.PropertyTwo; 
     default: 
     throw new Exception(); 
    } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
    throw new NotImplementedException(); 
    } 
} 
+0

這確實有用。我想知道是否有純粹的XAML方式來完成同樣的事情。 – Gus 2010-06-07 18:03:44

+0

你的意思是根本沒有代碼,並保持你的數據類是?使用ComplexValueClass類型的依賴項屬性編寫用戶控件,當依賴項屬性發生更改時,該屬性將自行失效。 – 2010-06-07 20:57:28

0

您需要在Complex類上使用INotifyPropertyChanged。如果您重新指定父項中的整個屬性,則唯一通知,如果您要綁定到它們,那麼該子類的屬性需要通知to0。