0

我正在尝试在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 样本有什么问题?

4

2 回答 2

1

在第一次转换中,您正在转换 a Color,其中的一个Grid正在转换 a 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();
    }
}
于 2013-03-02T21:56:44.547 回答
0

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

工作转换器:

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;
    }
}
于 2013-03-02T21:36:51.053 回答