2011-05-17 36 views
2
public static readonly DependencyProperty SingleGridLengthProperty = DependencyProperty.Register("SingleGridLength", typeof(double), typeof(MapConverter)); 

public class MapConverter : DependencyObject, INotifyPropertyChanged, IMultiValueConverter 
{ 
    public double SingleGridLength 
    { 
     get { return (double)GetValue(MapConverter.SingleGridLengthProperty); } 
     set 
     { 
      SetValue(MapConverter.SingleGridLengthProperty, value); 
      OnNotifyPropertyChanged("SingleGridLength"); 
     } 
    } 

<local:MapConverter x:Key="MapConverter" 
SingleGridLength="{Binding SingleGridLength, RelativeSource={RelativeSource Self}}" /> 

我有一組中的.xaml依賴項屬性:獲取而不是設置

我有每天屬性「漸」和返回值,但它的問題必然依賴屬性的轉換器從不「設置」價值。我可以在轉換器中使用依賴項屬性嗎?或者我應該以不同的方式來處理這個問題?提前致謝!

回答

2

首先,您的綁定無效。您將SingleGridLength屬性綁定到它自己。您需要將其綁定到另一個屬性/對象。

其次,您不應該在您的SingleGridLength屬性的setter中引發OnNotifyPropertyChanged。您只需要爲常規CLR屬性執行此操作。依賴屬性有一個綁定掛鉤的內置更改通知系統。

0

我建議使用基於IValueConverter的Converter? 轉換器應該只執行從輸入到輸出格式的計算。由轉換器

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 

返回的值將由您做了綁定的屬性來使用。

參見:http://msdn.microsoft.com/de-de/library/system.windows.data.ivalueconverter.aspx