2013-02-05 48 views
2

MultiBinding對背景屬性不起作用。當它被轉換時,背景只是轉到系統的默認顏色,而不是我在MultiValueConverter中設置的顏色。其他一切都是建立起來的。我的MultiBinding背景有什麼問題?WPF MultiBinding對背景屬性不起作用

<Style.Triggers> 
    <DataTrigger Binding="{Binding Source={StaticResource triggerResource}, 
            Path=MyIsSelected}"   
       Value="True"> 
     <Setter Property="Background"> 
      <Setter.Value> 
       <MultiBinding Converter="{StaticResource groupNameToBackgroundConv}"> 
        <Binding Path="Name" /> 
        <Binding Source="{StaticResource selectedGroupName}" Path="Name" /> 
       </MultiBinding> 
      </Setter.Value> 
     </Setter> 
    </DataTrigger> 
</Style.Triggers> 

而且我MultiValueConverter是

public class GroupNameToBackgroundConv : IMultiValueConverter 
{ 
    private const string DEFAULT_COLOR = "#B8CBE9"; 
    private const string SELECTED_COLOR = "#FFFF00";  

    public object Convert(object[] values, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     string groupName = values[0] as string; 
     string selectedGroupName = values[1] as string; 

     if (groupName == null) 
      return DEFAULT_COLOR; 

     if (selectedGroupName == null) 
      return DEFAULT_COLOR; 

     if (groupName == selectedGroupName) 
     { 
      return SELECTED_COLOR; 
     } 
     else 
     { 
      return DEFAULT_COLOR; 
     } 


    } // ends method 

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

} // ends class 
+0

您是否在轉換器中設置了一個斷點以查看是否正在調用Convert? – ChrisWue

+0

您是否還爲自己的風格設置了背景屬性(不僅適用於觸發器) ? –

+0

是的。正在調用轉換。 –

回答

3

我解決了這個問題。 當Convert返回一個值時,它需要是Brush,而不是字符串或Color,因爲 是控件Expander的背景屬性。 這是我的轉換器。

public class GroupNameToBackgroundConv : IMultiValueConverter 
{ 

    private Color DEFAULT_COLOR = (Color)ColorConverter.ConvertFromString("#B8CBE9"); 
    private Color SELECTED_COLOR = (Color)ColorConverter.ConvertFromString("#FFFF00");  

    public object Convert(object[] values, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     string groupName = values[0] as string; 
     string selectedGroupName = values[1] as string;   

     if (groupName == null) 
      return ColorOrBrush(DEFAULT_COLOR, targetType); 

     if (selectedGroupName == null) 
      return ColorOrBrush(DEFAULT_COLOR, targetType); 

     if (groupName == selectedGroupName) 
     { 
      return ColorOrBrush(SELECTED_COLOR, targetType); 
     } 
     else 
     { 
      return ColorOrBrush(DEFAULT_COLOR, targetType); 
     } 



    } // ends method 


    private object ColorOrBrush(Color c, Type targetType) 
    { 
     if (targetType == typeof(Color)) 
      return c; 
     else if (targetType == typeof(Brush)) 
      return new SolidColorBrush(c); 
     else 
      return null; 
    } 

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

} // ends class