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\$
1
  • 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\$ Jul 8, 2016 at 21:37

1 Answer 1

9
\$\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\$
5
  • 1
    \$\begingroup\$ Ah, C# 6 goodness, right! \$\endgroup\$ Jul 8, 2016 at 21:51
  • \$\begingroup\$ this looks really clean I must confess \$\endgroup\$
    – Tolani
    Jul 9, 2016 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, 2016 at 6:43
  • \$\begingroup\$ It's a bit short-sighted to discredit the answer (which you in fact did do) for "this amount" of chaining when this is exactly how chainable methods are designed to be used. I wonder how many chains you would allow--is 2 okay but 3 not? Also, the chained methods here (Split, Contains) hardly need "debugging", so there's no reason to worry about that. If the code doesn't work because it's not understood how "Contains" works, then you can always break out the chain to inspect how Contains works. But it's quite syntactically convenience to express the logic as shown. \$\endgroup\$
    – Jazimov
    Oct 18, 2022 at 20:41
  • \$\begingroup\$ @Jazimov it looks like you forgot to mention the other comment's author in your comment so he won't notice it. \$\endgroup\$
    – t3chb0t
    Oct 19, 2022 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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