【问题标题】:Binding an IValueConverter properly正确绑定 IValueConverter
【发布时间】:2013-03-15 14:18:38
【问题描述】:

我正在尝试创建某种带有发光背景的标签。为此,我决定在内容控件上使用样式。发光效果来自两个DropShadowEffects,我希望绑定到内容控件的Foreground PropertyForeground PropertyBrush 类型,DropShadowEffect.ColorColor 类型,所以我需要在这两者之间进行转换。

每当我尝试通过转换器设置发光颜色时,发光效果都会保持黑色。似乎转换器代码甚至从未通过。我确实在转换器中返回了预定义的颜色(无转换),甚至添加了 Debug.Break(),但无济于事。

你能告诉我我做错了什么吗,或者是否有其他更好的方法来实现带有发光背景的标签。

转换器:

public class ColorToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return null;

        if (value is Color)
        {
            Color color = (Color)value;
            BrushConverter bc = new BrushConverter();
            return bc.ConvertFrom(color);
        }

        Type type = value.GetType();
        throw new InvalidOperationException("Unsupported type ["+type.Name+"]");            
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

        if (value is Brush)
        {
            Brush brush = (Brush)value;
            BrushConverter bc = new BrushConverter();
            return bc.ConvertTo(brush, typeof(Color));
        }

        Type type = value.GetType();
        throw new InvalidOperationException("Unsupported type ["+type.Name+"]");            
    }
}

在资源字典中:

<local:ColorToBrushConverter x:Key="Color2BrushConverter" />

<Style x:Key="ContentControlGlowStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <Border>
                    <Border.Effect>
                        <DropShadowEffect
                                BlurRadius="15"
                                Color="{Binding Path=Foreground, Converter={StaticResource Color2BrushConverter}}"
                                ShadowDepth="2"
                                Direction="0"/>

                    </Border.Effect>

                    <TextBlock Name="Highlight" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Content}" Margin="10,5,0,0">
                        <TextBlock.Effect>  
                            <DropShadowEffect
                                BlurRadius="15"
                                Color="{Binding Path=Foreground,Converter={StaticResource Color2BrushConverter}}"
                                ShadowDepth="2"
                                Direction="0"/>

                        </TextBlock.Effect>

                    </TextBlock>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在 XAML 中:

<ContentControl Name="cc2" Style="{DynamicResource ContentControlGlowStyle}"
    FontSize="24"
    Foreground="LightBlue"
    Background="LightBlue"
    Content="some content to display"
    FontFamily="Verdana" />

【问题讨论】:

  • 你应该看看输出窗口,看看它是否显示任何异常。这可以让您更深入地了解正在发生的事情。
  • VS 不会抱怨输出窗口中的绑定。
  • 唯一想到的另一件事是设置颜色绑定的相对来源。
  • @TYY:将RelativeSource 设置为{RelativeSource AncestorType={x:Type ContentControl}}成功了。有一些小的额外问题,但这些是不同的主题,我可以处理。请写一个答案,以便我标记它。谢谢。
  • 没问题很高兴我能帮上忙。我在某个时间点遇到过类似的问题,我记得它与设置相对源有关。

标签: c# wpf data-binding ivalueconverter


【解决方案1】:

要解决您面临的问题,您需要将相对源设置为颜色绑定。知道这不是你的转换器问题的诀窍是它永远不会被调用,并且 VS 不会吐出任何错误,这意味着已经选择了默认值。

【讨论】:

    【解决方案2】:

    首先,您的转换器似乎倒退了——您正在将Brush 转换为Color,并且为此创建了ColorToBrushConverter

    另外,我不确定您为什么要以ContentControl 样式重新定义控件模板。您应该只设置一个DropShadowEffect,它的Color 绑定到ContentControlForeground

    试试这个:

    public class BrushToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var solidColorBrush = value as SolidColorBrush;
            if (solidColorBrush == null) return null;
    
            return solidColorBrush.Color;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    <local:BrushToColorConverter x:Key="BrushToColorConverter" />
    
    <Style x:Key="ContentControlGlowStyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect
                            BlurRadius="15"
                            Color="{Binding Foreground,RelativeSource={RelativeSource AncestorType=ContentControl}, Converter={StaticResource BrushToColorConverter}}"
                            ShadowDepth="2"
                            Direction="0"/>
                </Setter.Value>
           </Setter>
    </Style>
    

    像这样使用它

    <ContentControl 
        Foreground="Yellow" 
        Style="{DynamicResource ContentControlGlowStyle}">
        <TextBlock Text="TEST" FontSize="72"/>
    </ContentControl>
    

    【讨论】:

    • 转换器的名称有点混乱,是的:) 重新定义控件模板的原因是,简单地在文本块上添加阴影效果只会添加少量的发光。用边框包围它并为其添加阴影会给我想要的数量。 TYY 已经解决了绑定问题,但是 +1 作为您的解决方案是有用且正确的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 2012-04-06
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多