将转换器指定为MarkupExtension

时间:2016-05-08 11:49:00

标签: c# wpf xaml

我想实现值转换器来为RenderTransform指定Point。这工作正常,直到我只是实现IValueConverter接口。我知道我可以实现MarkupExtension类,以便每次都不为我的转换器声明分离的XAML资源。当我尝试实现这个时,我有

InvalidCastException:无法将类型为“System.Object”的对象强制转换为“System.Windows.Data.IValueConverter”。

转换器实施:

[ValueConversion(typeof(Point), typeof(Transform))]
public class PointToTransformConverter : MarkupExtension, IValueConverter
{
    private PointToTransformConverter _instance = null;

    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        CultureInfo culture)
    {
        var point = (Point)value;

        return new TransformGroup
        {
            Children = new TransformCollection
            {
                new TranslateTransform(point.X, point.Y)
            }
        };
    }

    public object ConvertBack(
        object value, 
        Type targetType, 
        object parameter, 
        CultureInfo culture)
    {
        var transform = value as TransformGroup;
        if (transform?.Children.Count > 0)
        {
            var translateTransform = transform.Children[0] as TranslateTransform;
            if (translateTransform != null)
            {
                return new Point(
                    translateTransform.X, 
                    translateTransform.Y);
            }
        }

        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _instance ?? (_instance = new PointToTransformConverter());
    }
}

XAML用法:

<local:PathControl x:Class="PathToWiringTube.PathView"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:local="clr-namespace:PathToWiringTube"
                   xmlns:view="clr-namespace:PathToWiringTube.View"
                   xmlns:vm="clr-namespace:PathToWiringTube.VM"
                   xmlns:crocodile="clr-namespace:Crocodile;assembly=Crocodile"
                   mc:Ignorable="d" 
                   d:DataContext="{d:DesignInstance vm:PathVm}">
    <local:PathControl.Resources>
        <crocodile:PointToTransformConverter x:Key="pointToTransformConverter"/>
    </local:PathControl.Resources>
    <Canvas>
        <ItemsControl x:Name="itemsMarkers" ItemsSource="{Binding Markers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <view:SplineDensityMarker Width="5" RenderTransform="{Binding Position, Converter={StaticResource pointToTransformConverter}}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Canvas>
</local:PathControl>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

请您尝试下面的代码并检查异常是否即将到来? 我纠正了它。实例应该是静态的和xaml,您可以直接引用转换器而不是将其称为静态资源。这是使用 MarkupExtension 的主要用途。下面的代码应该可以正常工作。如有任何问题,请恢复。

转换器:

[ValueConversion(typeof(Point), typeof(Transform))]
public class PointToTransformConverter : MarkupExtension, IValueConverter
{
    private static PointToTransformConverter _instance = null;

    public object Convert(
        object value, 
        Type targetType, 
        object parameter, 
        CultureInfo culture)
    {
        var point = (Point)value;

        return new TransformGroup
        {
            Children = new TransformCollection
            {
                new TranslateTransform(point.X, point.Y)
            }
        };
    }

    public object ConvertBack(
        object value, 
        Type targetType, 
        object parameter, 
        CultureInfo culture)
    {
        var transform = value as TransformGroup;
        if (transform?.Children.Count > 0)
        {
            var translateTransform = transform.Children[0] as TranslateTransform;
            if (translateTransform != null)
            {
                return new Point(
                    translateTransform.X, 
                    translateTransform.Y);
            }
        }

        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _instance ?? (_instance = new PointToTransformConverter());
    }
}

Xaml:

<local:PathControl x:Class="PathToWiringTube.PathView"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:local="clr-namespace:PathToWiringTube"
                   xmlns:view="clr-namespace:PathToWiringTube.View"
                   xmlns:vm="clr-namespace:PathToWiringTube.VM"
                   xmlns:crocodile="clr-namespace:Crocodile;assembly=Crocodile"
                   mc:Ignorable="d" 
                   d:DataContext="{d:DesignInstance vm:PathVm}">
    </local:PathControl.Resources>
    <Canvas>
        <ItemsControl x:Name="itemsMarkers" ItemsSource="{Binding Markers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <view:SplineDensityMarker Width="5" RenderTransform="{Binding Position, Converter={crocodile:PointToTransformConverter}}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Canvas>
</local:PathControl>

答案 1 :(得分:0)

我在另一个环境中检查了我的代码并检测到它有效。我已经完成了Visual Studio版本不匹配的研究并找到了它(14.0.23107.0 D14REL和14.0.24720.00 Update1)。在我的旧环境中安装最新的Visual Studio Update 2后,一切正常。