Wpf 使用UniversalValueConverter(或任何转换器)将字符串绑定到Path.Data时出现问题

Wpf 使用UniversalValueConverter(或任何转换器)将字符串绑定到Path.Data时出现问题,wpf,data-binding,mvvm,valueconverter,pathgeometry,Wpf,Data Binding,Mvvm,Valueconverter,Pathgeometry,我尝试使用字符串属性来更新路径数据,使用ValueConverter。我认为最简单的方法就是展示它:(我使用WPF和MVVM模式,也使用MVVM灯) 在我的ViewModel中: private string _icon = "F1 M 34.7541,26.4939L 20.5932,1.72809C 19.9132,0.624023 18.9211,0.0480042 17.6171,0C 16.265,0.0480042 15.2729,0.624023 14.6409,1.728

我尝试使用字符串属性来更新路径数据,使用ValueConverter。我认为最简单的方法就是展示它:(我使用WPF和MVVM模式,也使用MVVM灯)

在我的ViewModel中:

    private string _icon = "F1 M 34.7541,26.4939L 20.5932,1.72809C 19.9132,0.624023 18.9211,0.0480042 17.6171,0C 16.265,0.0480042 15.2729,0.624023 14.6409,1.72809L 0.480042,26.4939C 0.151978,27.0559 -0.00799561,27.6424 0,28.2534C 0.0289917,29.2073 0.378998,29.9982 1.05005,30.6259C 1.72107,31.2536 2.53915,31.579 3.50421,31.6022L 31.7299,31.6022C 32.693,31.5848 33.503,31.271 34.1601,30.6607C 34.8171,30.0504 35.1591,29.248 35.1861,28.2534C 35.1861,27.6424 35.0421,27.0559 34.7541,26.4939 Z M 15.0729,8.06448L 20.2092,8.06448L 20.2092,19.7072L 15.0729,19.7072L 15.0729,8.06448 Z M 17.665,22.4372C 18.4991,22.4576 19.1832,22.7468 19.7172,23.3048C 20.2512,23.8628 20.5272,24.5674 20.5453,25.4186C 20.5272,26.2444 20.2512,26.9266 19.7172,27.4652C 19.1832,28.0039 18.4991,28.2829 17.665,28.3022C 16.831,28.2829 16.147,28.0039 15.6129,27.4653C 15.0789,26.9266 14.8029,26.2444 14.7849,25.4186C 14.8029,24.546 15.0789,23.8353 15.6129,23.2864C 16.147,22.7376 16.831,22.4545 17.665,22.4372 Z " ;
    public string Icon
    {
        get { return _icon; }
        set
        {
            _icon = value;
            RaisePropertyChanged(() => Icon);
        }
    }
这个字符串的目的是我可以为我的应用程序中的不同事件用不同的字符串(图标)更新图标(XAML图标)

我认为:

<UserControl.Resources>
    <local:UniversalValueConverter x:Key="UniversalValueConverter"/>
<UserControl.Resources>
我不太熟悉值转换器的工作原理,也不太熟悉如何使用它们。但我已经尝试了不同的价值转换器从网络上为这一点。(我以前在其他情况下成功地使用了值转换器)这两个都像UniversalValueConverter,但也像StringToPathGeometryConverter。什么都没用。运行应用程序时,不显示任何图标。但也没有抛出异常

我还尝试将Icon属性设置为PathGeometry,并在没有值转换器的情况下绑定它,但它也不起作用


有人知道吗?

这可能是由于绑定未在xaml文件中正确设置。检查VisualStudio输出窗口,查看运行时是否存在任何绑定错误

要使xaml中的绑定正常工作,必须将根窗口对象的DataContext属性设置为正确的对象或xaml元素/控件

在您的情况下,必须确保ViewModel的
图标
属性流动并正确绑定到xaml
路径
元素

有关数据绑定的详细信息,请参阅:


检查Visual Studio输出窗口,查看在运行时是否存在任何
绑定
错误..是!我收到以下消息:System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“我的命名空间”(HashCode=59603417)上找不到“图标”属性。BindingExpression:路径=图标;DataItem='MyNamespace'(HashCode=59603417);目标元素为“路径”(名称=“”);目标属性是'Data'(类型'Geometry'),但它实际上并没有告诉我太多!我想我理解这个错误。这是因为绑定位于datagrid中,datagrid具有ItemSource。因此,它试图在项目源中找到我的属性,而不是。。
                <DataGridTemplateColumn Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                            <Viewbox RenderTransformOrigin="0.5,0.5" Margin="3" MaxHeight="10">
                                <Viewbox.RenderTransform>
                                    <TransformGroup>
                                    </TransformGroup>
                                </Viewbox.RenderTransform>
                            <Path Stretch="Uniform" Fill="Red" Data="{Binding Path=Icon, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=UniversalValueConverter}}"/>
                            </Viewbox>                        
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
public class UniversalValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // obtain the conveter for the target type
        TypeConverter converter = TypeDescriptor.GetConverter(targetType);

        try
        {
            // determine if the supplied value is of a suitable type
            if (converter.CanConvertFrom(value.GetType()))
            {
                // return the converted value
                return converter.ConvertFrom(value);
            }
            else
            {
                // try to convert from the string representation
                return converter.ConvertFrom(value.ToString());
            }
        }
        catch (Exception)
        {
            return value;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}