5
\$\begingroup\$

Here is a simple WPF converter method designed, where the object is a string, and the parameters are a string, which contain a set of comma separated values. The program return true if the list has the value in and false otherwise.

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            string temp = parameter as string;
            List<String> list = temp.Split(',').ToList();
            string x = value as string;

            return list.Contains(x);
        }
        catch (Exception)
        {

            throw;
        }
    }

Why I am doing this

I was making a WPF Tab Controller, that looks at the type of objects that it can load and opens up different views based on that information. I cannot change the types of stations that are being supplied to me. Several of these stations share a view between all of them even though their type is different, I am using this converter in a Data Trigger in order to show the same view for this list of stations.

            <DataTrigger Binding="{Binding StationState.StationType, Converter={StaticResource BindingInListConvertor}, ConverterParameter={StaticResource FinishingString}}" Value="True">
                <Setter Property="ContentTemplate" Value="{StaticResource FinishingTemplate}" />
            </DataTrigger>

The only other ways I could think of to do this are a multibinding data-trigger, or just a whole lot of them, and figured this would be best because the the possibility of future functionality.

This is my solution for it, is their a better/simpler/cooler way to acomplish this, thanks.

\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to CR! With (much) more information and details about exactly what you're accomplishing, reviewers could not only shred your converter code, but also recommend completely different approaches. What makes you think a converter is the best way to do what you're doing? \$\endgroup\$ – Mathieu Guindon Jul 8 '16 at 21:37
8
\$\begingroup\$

This could be a better/cooler way:

return (parameter as string)?.Split(',').Contains(value as string) ?? (object)false;

Your example is so short that I don't know what else I could say. You are looking for a short solution, so here is one.

\$\endgroup\$
  • 1
    \$\begingroup\$ Ah, C# 6 goodness, right! \$\endgroup\$ – Mathieu Guindon Jul 8 '16 at 21:51
  • \$\begingroup\$ this looks really clean I must confess \$\endgroup\$ – Siobhan Jul 9 '16 at 15:56
  • 2
    \$\begingroup\$ It's smart, and I don't want to discredit the answer. Good job answering the question. However, I would not allow this amount of chaining to pass a code review in my company. Debugging it would be difficult without refactoring to something close to the OP's original sample. \$\endgroup\$ – Trevor Ash Jul 10 '16 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.