【问题标题】:IValueConverter in ResourceDictionaryResourceDictionary 中的 IValueConverter
【发布时间】:2013-02-17 05:44:33
【问题描述】:

我正在尝试在ResourceDictionary 中使用转换器。这就是我的代码:

<Window x:Class="Metro.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cnv="clr-namespace:Metro.converters">
    <Window.Resources>
        <cnv:DarkenColorConverter x:Key="Darken" />
        <Color x:Key="Red">#FF0000</Color>
        <SolidColorBrush Color="{StaticResource Red}"
                         x:Key="Accent" />
        <SolidColorBrush Color="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}"
                         x:Key="DarkAccent" />
    </Window.Resources>
    <StackPanel>
        <Grid Background="{StaticResource Accent}">
            <TextBlock>grid 1</TextBlock>
        </Grid>
        <Grid Background="{StaticResource DarkAccent}">
            <TextBlock>grid 2</TextBlock>
        </Grid>
    </StackPanel>
</Window>

这是转换器:

public class DarkenColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Blue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Gray;
    }
}

但不知何故,它不起作用。只要我直接在Grid 中使用转换器,一切正常:

    <Grid Background="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}">
        <TextBlock>grid 2</TextBlock>
    </Grid>

第一个 xaml 样本有什么问题?

【问题讨论】:

    标签: c# wpf visual-studio-2010 xaml .net-3.5


    【解决方案1】:

    在第一次转换中,您正在转换ColorGrid 中的转换正在转换SolidColorBrush

    您还必须修改转换器以接受颜色。

    public class DarkenColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double percentage = 0.8;
            if (parameter != null)
            {
                double.TryParse(parameter.ToString(), out percentage);
            }
    
            if (value is SolidColorBrush)
            {
                Color color = (value as SolidColorBrush).Color;
                return new SolidColorBrush(Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage)));
            }
            else if (value is Color)
            {
                Color color = (Color)value;
                return Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage));
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    【讨论】:

    • 再次感谢您的帮助。几分钟前我已经发现了我的错误,但无法接受我自己的答案。
    • 是的,抱歉直到我发帖才看到你的答案,很高兴你把一切都整理好了。快乐编码:)
    【解决方案2】:

    问题是错误的转换器返回类型。

    工作转换器:

    public class DarkenColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Colors.Blue;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,     System.Globalization.CultureInfo culture)
        {
            return Colors.Gray;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2014-07-31
      • 2012-10-26
      相关资源
      最近更新 更多