Resolved Help make this converter work, please.

Scottintexas

Well-known member
Joined
Jun 21, 2018
Messages
47
Location
Texas
Programming Experience
5-10
I have never used a converter until I tried today and I didn't get it right.

The Converter Class:
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            StopBits sb = (StopBits)value;
            string stops = string.Empty;
            switch (sb)
            {
                case StopBits.None:
                    stops = "None";
                    break;

                case StopBits.One:
                    stops = "One";
                    break;

                case StopBits.OnePointFive:
                    stops = "OnePointFive";
                    break;

                case StopBits.Two:
                    stops = "Two";
                    break;
            }
            return stops;   
        }

The XAML with the Binding:
           <TextBlock Grid.Column="1"
                       Grid.Row="3"
                       Style="{StaticResource ComportPropertiesStyle}"
                       Text="{Binding Converter={StaticResource StopBitConverter}}">
            </TextBlock>

The XAML resources are in a Resource Library which is referenced at the top of the View XAML <ResourceDictionary Source="./ViewResources.xaml" />

And finally the resource reference looks like this <vm:StopBitConverter x:Key="StopBitConverter"/> Which is suspicious because it just looks wrong.

The project wont build. The error is "The resource "StopBitConverter" has an incompatible type" at the binding line which would be line 4 in XAML the code above.
Also "The name "StopBitConverter" does not exist in the namespace "clr-namespace:Izod_Impact.ViewModels" which is not true. It is used by all of the views. But I think once the incompatible type issue is resolved, the other error will go away. Maybe not.

How do I do this right?
 
Just doing a quick scan, and it looks like it matches up with what the docs tell you to do:
 
Never mind. There was also an error at line 318 of the resources file. There was an errant /> laying there. ???? Don't know where it even came from. Once I deleted it, all the other errors went away including the error I wrote about. The project built, I got an exception because the object type of my converter was for a parent object. I had to drill down one level to get to the object of intertest. Now it works.

Fixed XAML:
            ComPortViewModel portViewModel = (ComPortViewModel)value;   
            StopBits sb = portViewModel.StopBits; 
            string stops = string.Empty;
            switch (sb)
            {
...

Thanks. I would still be interested in hearing from you if you have any suggestions regarding converters.

Scott
 
Sometime it is faster to just have the view model already expose the converted value, instead of the framework having to do the roundtrip of binding to a view model value, create an instance of the converter, pass in the bound value, and the get back the converter value.

Sure, having the converter makes for more modular and cleaner code that has just a single responsibility, but it can also be a pain to keep mental model of all the code. If the view models ' sole reason d'etre is to service the UI, let it get into the minutea of converting some value to a gradient brush or an image as well.
 

Latest posts

Back
Top Bottom