HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Find a value in a comma delimited String

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
commadelimitedvaluefindstring

Problem

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


                
            


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.

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#134321, answer score: 9

Revisions (0)

No revisions yet.