简体   繁体   中英

IValueConverter in ResourceDictionary

I'm trying to use a Converter inside a ResourceDictionary . That's the code I have:

<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>

Here's the converter:

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;
    }
}

But somehow it's not working. As soon as I use the converter inside the Grid directly, everything works fine:

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

What`s wrong with the first xaml sample?

In the first conversion you are converting a Color , the one in the Grid is converting a SolidColorBrush .

You will have to modify your converter to accept Color also.

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();
    }
}

The problem was the wrong converter return type.

Working converter:

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM