2015-06-19 32 views
2

我寫了一個轉換器,讓通價值

  • ValueBool
  • ParameterString

我用這樣的:

BorderBrush="{Binding IsSelected, 
         Converter={StaticResource BoolToColorBrushConverter}, 
         ConverterParameter='#ff00bfff;#0000bfff'}" 

如果ValueTrue,則轉換器從參數中的第一個顏色十六進制代碼返回ColorBrush否則返回第二個顏色十六進制代碼中的ColorBrush

我的轉換工作非常好我想知道我怎麼可以用這樣的:

<Color x:Key="MyColor1">#66bb66</Color> 

-------------------- 

BorderBrush="{Binding IsSelected, 
         Converter={StaticResource BoolToColorBrushConverter}, 
         ConverterParameter=#ff00bfff;{StaticResource MyColor1}}" 

結果在設計模式:

enter image description here

結果在運行:

enter image description here

但我需要的StaticResource的顏色的十六進制代碼在我的參數是這樣的:

Parameter: "#ff00bfff;#66bb66" 

我的問題是如何傳遞的結合字符串我ConverterParameter一個StaticResource值???

您的解決方案是什麼?

+0

使整個參數字符串成爲資源*或*向您的轉換器添加屬性並創建具有不同屬性值的多個轉換器對象*或*使用帶有IMultiValueConverter的MultiBinding。 – Clemens

+0

你不能那樣做。相反,使用您可以用來綁定的顏色添加一個屬性,您可以從中調用該轉換器。 – helb

+0

這看起來像是一個'MultiValueConverter'的完美任務。 – almulo

回答

1

我知道這是一個有點晚了,但我希望這可以幫助已故用戶:

這裏是轉換器代碼:

public class BoolToBorderBrushConverter : IValueConverter 
{ 
    public SolidColorBrush TrueColor { get; set; } 

    public SolidColorBrush FalseColor { get; set; } 

    // this example compares a binding property (string) with 1 parameter (also in string) 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null && parameter != null) 
     { 
      if (String.Compare(value.ToString(), parameter.ToString(), true) == 0) 
      { 
       return this.TrueColor; 
      } 
      else 
      { 
       return this.FalseColor; 
      } 
     } 
     return this.FalseColor; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

那麼您可以在這樣的XAML配置的轉換器(在ResourceDictionary的部分):

<ResourceDictionary> 
    <local:BoolToBorderBrushConverter x:Key="BrushConverter" TrueColor="{StaticResource MyTrueColor}" FalseColor="Transparent"> 
</ResourceDictionary> 

,這是你如何使用轉換器:

<Border BorderBrush="{Binding MyProperty, Converter={StaticResource BrushConverter}, ConverterParameter=ABC}"/>