0

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

在我的视图模型中:

    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>

我有一个数据网格,其中一列包含以下内容:

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

我的 ValueConverter 是这样的:

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

我对价值转换器的工作原理或如何使用它们不太熟悉。但是我为此尝试了来自网络的不同值转换器。(我之前在其他情况下成功使用过值转换器)既喜欢这个 UniversalValueConverter,也喜欢 StringToPathGeometryConverter。没有任何工作。当我运行我的应用程序时,没有显示任何图标。但是也没有抛出异常。

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

有谁知道?

4

1 回答 1

1

This could be due to the Binding DataContext not set correctly in your xaml file. Check your Visual Studio Output window to see if there are any Binding errors at run time.

For the Bindings in xaml to work properly, you have to set the DataContext property of either your root Window object to a correct object or to the xaml element/control correctly.

In your case, you will have to make sure that the Icon property of your ViewModel flows and binds correctly to your xaml Path element.

For more information on DataBinding, See:

于 2014-05-07T07:03:14.703 回答