首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确绑定IValueConverter

正确绑定IValueConverter
EN

Stack Overflow用户
提问于 2013-03-15 22:18:38
回答 2查看 1.4K关注 0票数 3

我正在尝试创建一种具有发光背景的标签。为了实现这一点,我决定在content控件上使用样式。发光效果来自两个DropShadowEffects,我希望将它们绑定到content控件的Foreground PropertyForeground Property的类型是Brush,而DropShadowEffect.Color的类型是Color,所以我需要在这两者之间进行转换。

每当我尝试通过转换器设置发光颜色时,发光效果保持为黑色。似乎转换器代码从来没有被传递过。我确实在转换器中返回了一个预定义的颜色(没有转换),甚至添加了一个Debug.Break(),但都没有用。

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

转换器:

代码语言:javascript
复制
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+"]");            
    }
}

在资源字典中:

代码语言:javascript
复制
<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中:

代码语言:javascript
复制
<ContentControl Name="cc2" Style="{DynamicResource ContentControlGlowStyle}"
    FontSize="24"
    Foreground="LightBlue"
    Background="LightBlue"
    Content="some content to display"
    FontFamily="Verdana" />
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-15 23:02:04

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

票数 1
EN

Stack Overflow用户

发布于 2013-03-15 23:10:12

首先,您的转换器似乎是倒退的--您正在将Brush转换为Color,并且您已经创建了一个ColorToBrushConverter来完成此操作。

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

试着这样做:

代码语言:javascript
复制
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();
    }
}

代码语言:javascript
复制
<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>

像这样使用它

代码语言:javascript
复制
<ContentControl 
    Foreground="Yellow" 
    Style="{DynamicResource ContentControlGlowStyle}">
    <TextBlock Text="TEST" FontSize="72"/>
</ContentControl>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15434723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档