我有一个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