1

我做了一个从字符串到颜色再返回的转换器,它在运行时工作正常,但在编辑器上它只是抛出“令牌无效”。错误并阻止编辑器出现,真的很烦人,因为它阻止我使用可视化编辑器。

我从扩展的 WPF 工具包中为 ColorPicker 制作了转换器。

这是转换器代码:

public class MaddoColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Color color = Colors.Black;

        if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
        {
            string c = value.ToString();
            var convertedColor = ColorConverter.ConvertFromString(c);
            if (convertedColor != null)
            {
                color = (Color) convertedColor;
            }
        }

        return color;

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            Color color = (Color)value;
            Debug.WriteLine(color.ToString());
            return color.ToString();
        }
        return string.Empty;
    }
}

以下是来自 xaml 表单的一些相关片段:

<Window.Resources>        
    <wpfCatalog:MaddoColorConverter x:Key="ColorConverter" />
</Window.Resources>

<xctk:ColorPicker Grid.Row="3" Grid.Column="2" SelectedColor="{Binding ColoreTestoRGB, Converter={StaticResource ColorConverter}}"/>
4

1 回答 1

2

您需要向您的MaddoColorConverter. 例如,如果绑定失败,WPF 将传递DependencyProperty.UnsetValue给您的转换器。您的转换器不检查这种情况,而只是将传递的任何内容转换为字符串。像这样更改您的转换器(注意我只更新了Convert方法,没有触及ConvertBack可能需要修复的方法,但这与这个问题无关):

public class MaddoColorConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        Color color = Colors.Black;

        if (value != null && value != DependencyProperty.UnsetValue && value is string && !String.IsNullOrWhiteSpace((string) value)) {
            string c = (string) value;
            object convertedColor = null;
            try {
                convertedColor = ColorConverter.ConvertFromString(c);
            }
            catch (Exception ex) {
                throw new FormatException($"String {c} does not represent a valid color", ex);
            }
            if (convertedColor != null) {
                color = (Color) convertedColor;
            }
        }

        return color;
    }
}

如果由于某种原因预计在设计时具有无效的颜色值 - 在设计时不要抛出异常,如下所示:

private static readonly DependencyObject _dummy = new DependencyObject();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    System.Windows.Media.Color color = Colors.Black;

    if (value != null && value != DependencyProperty.UnsetValue && value is string && !String.IsNullOrWhiteSpace((string) value)) {
        string c = (string) value;
        object convertedColor = null;
        try {
            convertedColor = ColorConverter.ConvertFromString(c);
        }
        catch (Exception ex) {
            if (!DesignerProperties.GetIsInDesignMode(_dummy)) {
                throw new FormatException($"String {c} does not represent a valid color", ex);
            }
        }
        if (convertedColor != null) {
            color = (Color) convertedColor;
        }
    }

    return color;
}
于 2017-03-20T09:33:36.643 回答