2017-10-28 148 views
0

我有一個MultiValueConverter返回兩個SolidColoBrush之一作爲values []元素傳遞,這取決於作爲values [] elment傳遞的bool。MultiValueConverter與Color屬性一起使用,但不與Background屬性一起使用

所有值[]元素都是DependencyProperty。

問題是,當我通過這個MultiValueConverter將Background屬性綁定到這些SolidColorBrush時,我得到了一個轉換錯誤。

如果我將SolidColorBrush.Color屬性綁定到這些SolidColorBrush(這對我來說似乎是一個錯誤,除非在某處有隱式轉換),在運行時一切正常,但設計器不顯示所需的背景,它始終顯示背景與我的DependencyProperty的「假」值相關聯,即使我將該屬性設置爲true。

的代碼是這樣的:

XAML - 用戶控件

<UserControl.Resources> 
    <local:State2BackgroundConverter x:Key="state2BackgroundConverter"/> 
</UserControl.Resources> 

<Grid.Background> 
    <SolidColorBrush> 
      <SolidColorBrush.Color> 
       <MultiBinding Converter="{StaticResource state2BackgroundConverter}"> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="Status"/> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushOFF"/> 
        <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushON"/> 
       </MultiBinding> 
      </SolidColorBrush.Color> 
     </SolidColorBrush> 
</Grid.Background> // <--- This works at runtime but not at design time 

<Grid.Background> 
     <MultiBinding Converter="{StaticResource state2BackgroundConverter}"> 
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="Status"/> 
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushOFF"/> 
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushON"/> 
     </MultiBinding> 
</Grid.Background> // <--- This generates a cast error 

XAML - 窗口

<LightControls:MyButton Margin="1,0,0,0" Height="80" HorizontalAlignment="Left" Width="80" BrushON="#FF7CFEFF" BrushOFF="#FF0096FF" Status="True"/> 

C#

public class Status2ColorConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if ((bool)values[0] == true) 
      return ((SolidColorBrush)values[2]); 
     else 
      return ((SolidColorBrush)values[1]); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     return (null); 
    } 
} 

private static readonly DependencyProperty StatusProperty = 
DependencyProperty.Register("Status", 
            typeof(bool), 
            typeof(MyButton), 
            new PropertyMetadata(tre) 
            ); 
    public bool Status 
    { 
     get { return (bool)this.GetValue(StatusProperty); } 
     set { this.SetValue(StatusProperty, value); } 
    } 

    private static readonly DependencyProperty BrushONProperty = DependencyProperty.Register("BrushON", 
            typeof(SolidColorBrush), 
            typeof(MyButton), 
            new PropertyMetadata(new SolidColorBrush(Colors.Cyan)) 
            ); 
    public SolidColorBrush BrushON 
    { 
     get { return (SolidColorBrush)this.GetValue(BrushONProperty); } 

     set { this.SetValue(BrushONProperty, value); } 
    } 

    private static readonly DependencyProperty BrushOFFProperty = DependencyProperty.Register("BrushOFF", 
            typeof(SolidColorBrush), 
            typeof(MyButton), 
            new PropertyMetadata(new SolidColorBrush(Colors.Black)) 
            ); 
    public SolidColorBrush BrushOFF 
    { 
     get { return (SolidColorBrush)this.GetValue(BrushOFFProperty); } 

     set { this.SetValue(BrushOFFProperty, value); } 
    } 

所以我的問題是:

  1. 我爲什麼要綁定SolidColorBrush.Color的屬性,即使我的BrushON/BrushOFF回報的SolidColorBrush而不是SolidColorBrush.Color?
  2. 我怎樣才能在設計時間讓它工作? (注意:我項目中的所有其他依賴屬性,包括那些具有自定義ValueConverter而非MultiValueConverter的依賴屬性,在設計時工作得很好,甚至其他背景屬性)

在此先感謝!

+0

這一切似乎過於複雜,當你可以簡單地擁有的PropertyChanged回調狀態屬性,它直接設置網格背景兩種顏色之一。 – Clemens

+0

我對WPF相當陌生,所以請耐心等待,我使用propertyChanged回調(這裏沒有顯示)來執行一些代碼,但這不會在設計時顯示期望的背景(它在運行時),所以我必須問你是否可以建議一些資源,我可以在這裏瞭解到我在這裏失蹤的內容。但是我會留下這個問題,因爲我想知道錯誤在哪裏,即使執行起來更容易。非常感謝你! – Boltzmann

+0

另外需要注意的是,如果有一個名爲'ColorON'的屬性,我不會指望它是'SolidColorBrush'類型。它的類型應該是'Color',這也會使你的代碼變得更簡單。 – Clemens

回答

0

對於那些遇到同樣問題的人,我找到了回答我的問題的第2點。 與IValueConverter不同的是,IMultiValueConverter無法在設計時評估依賴屬性,即使它具有默認值。

這裏是給了我正確的觀念鏈接:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/9b902711-2c45-49f9-9478-b36f1e842f0b/imultivalueconverter-error-crashing-visual-studio-at-design-time?forum=wpf

在這條「爲」運營商提出的解決方案,因爲它sanifies空變量。

在我的代碼中存儲依賴屬性值的變量在設計時不應該爲空(當依賴屬性具有默認值時,實際上當使用相同的依賴屬性時,因爲IValueConverter的值一切正常),所以右要做的事情是通過檢查values []是否爲null並在此情況下分配默認返回值來防止這種設計器不當行爲。

爲例(可能有許多聰明的方法來獲得相同的結果):

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     string s = values[0] as string; 
     if (s==null) 
     { 
      return (SomeDefaultValue); 
     } 
     else 
     { 
      Actually do something 
     } 
    } 
相關問題