Question

I'm trying to use a string property to update the path data, using a ValueConverter. I think the easiest way is just to show it: (I'm using WPF and MVVM pattern. Also using MVVM Light)

In my 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);
        }
    }

The purpose of this string is that I can update the icon (XAML icon) with different string (icons), for different events in my application.

In my View:

<UserControl.Resources>
    <local:UniversalValueConverter x:Key="UniversalValueConverter"/>
<UserControl.Resources>

I have a data grid, where one of the columns contains this:

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

My ValueConverter is like this:

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

I'm not so familiar with how value converters works, or how to use them. But I have tried different value converters from the web for this. (I have used value converters successfully before in other cases) Both like this UniversalValueConverter, but also StringToPathGeometryConverter. Nothing is working. When I run my application, no icon are shown. But there is no exception thrown either.

I have also tried to make the Icon property to be a PathGeometry, and to bind it without a value converter, but it didn't work either.

Anyone knows?

Was it helpful?

Solution

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top