2017-10-28 177 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 
     } 
    } 
相关问题