我有一個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); }
}
所以我的問題是:
- 我爲什麼要綁定SolidColorBrush.Color的屬性,即使我的BrushON/BrushOFF回報的SolidColorBrush而不是SolidColorBrush.Color?
- 我怎樣才能在設計時間讓它工作? (注意:我項目中的所有其他依賴屬性,包括那些具有自定義ValueConverter而非MultiValueConverter的依賴屬性,在設計時工作得很好,甚至其他背景屬性)
在此先感謝!
這一切似乎過於複雜,當你可以簡單地擁有的PropertyChanged回調狀態屬性,它直接設置網格背景兩種顏色之一。 – Clemens
我對WPF相當陌生,所以請耐心等待,我使用propertyChanged回調(這裏沒有顯示)來執行一些代碼,但這不會在設計時顯示期望的背景(它在運行時),所以我必須問你是否可以建議一些資源,我可以在這裏瞭解到我在這裏失蹤的內容。但是我會留下這個問題,因爲我想知道錯誤在哪裏,即使執行起來更容易。非常感謝你! – Boltzmann
另外需要注意的是,如果有一個名爲'ColorON'的屬性,我不會指望它是'SolidColorBrush'類型。它的類型應該是'Color',這也會使你的代碼變得更簡單。 – Clemens