2013-07-09 59 views
1

我需要一些幫助來找出我在Silverlight 5中實現MultiBooleanConverter的問題。我有實現,但獲取正確的引用會給我帶來一些麻煩。Silverlight 5中的PresentationFramework

這是我的初學者代碼。

XAML:

<telerikRibbonView:RadRibbonButton.Visibility> 
    <MultiBinding Converter="{StaticResource MultiBooleanToVisibilityConverter}"> 
      <Binding Path="Path1" /> 
      <Binding Path="Path2" /> 
    </MultiBinding> 
</telerikRibbonView:RadRibbonButton.Visibility> 

轉換器(Credit):我有

class MultiBooleanToVisibilityConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, 
          Type targetType, 
          object parameter, 
          System.Globalization.CultureInfo culture) 
    { 
     bool visible = true; 
     foreach (object value in values) 
      if (value is bool) 
       visible = visible && (bool)value; 

     if (visible) 
      return System.Windows.Visibility.Visible; 
     else 
      return System.Windows.Visibility.Collapsed; 
    } 

    public object[] ConvertBack(object value, 
           Type[] targetTypes, 
           object parameter, 
           System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

問題是IMultiValueConverter接口駐留在命名空間System.Windows.Data,它駐留在PresentationFramework DLL,我無法在我的Silverlight項目中添加爲參考,因爲它不是針對Silverlight構建的。

我很抱歉,如果我完全失去了明顯的東西。我如何在Silverlight中使用IMultiValueConverter?是否有我需要的不同的DLL?

而且,我的所有其他接口實現IValueConverter住在System.Windows.Datac:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Libraries\Client\System.Windows.Data DLL拉這不是我需要IMultiValueConverter大會。然而,具有不明確的System.Windows.Data命名空間不應該是一個問題,因爲我可以使用Alias綁定來解決歧義。我只需要弄清楚如何在Silverlight中獲得IMultiValueConverter

回答

2

不幸的是Silverlight沒有Multibinding場景的框架實現,所以你必須自己編寫更多的代碼。

這裏是包括一些漂亮乾淨的代碼來做到這一點雖然文章 - http://www.scottlogic.com/blog/2010/05/10/silverlight-multibinding-solution-for-silverlight-4.html

它包括代碼來顯式定義相同的接口,然後你可以用上面的代碼中使用:

public interface IMultiValueConverter 
    { 
     object Convert(object[] values, Type targetType, object parameter, 
         CultureInfo culture); 

     object[] ConvertBack(object value, Type[] targetTypes, object parameter, 
          CultureInfo culture);  
    } 
+0

哦,球......好吧。謝謝(你的)信息! – tnw