We help IT Professionals succeed at work.

WPF binding case/switch statement?

zkeown asked
Medium Priority
Last Modified: 2013-11-12
This is either really easy or impossible.  

I'm binding a ListBox to a collection of contacts.  I'd like to have a boolean on each contact saying if the person is an employee or not, and then when displaying in the listbox, show "Employee" in a TextBlock if so, but leave it empty if not (or say "Non-Employee", I haven't decided).  

Is this kind of conditional binding supported?
Watch Question

add a event handler for indexchange of ListBox; and ListBox will bind to some kind of datasource.
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            if (this.listBox1.SelectedIndex != -1)
                XmlNode node = this.listBox1.SelectedItem as XmlNode;
                if (node != null)
                    this.textBox1.Text = node.Attributes["Employee"].Value == "true" ? "Employee" : "Non-Employee";
            <XmlDataProvider x:Key="MyData" XPath="/Contact">
                    <Contact xmlns="">
                        <Item ID="0001" Name="Tom" Employee="true"/>
                        <Item ID="0002" Name="Jerry" Employee="true"/>
                        <Item ID="0003" Name="John" Employee="true"/>
                        <Item ID="0004" Name="Joan" Employee="true"/>
                        <Item ID="0005" Name="Jim" Employee="false"/>
                        <Item ID="0006" Name="Chris" Employee="false"/>
        <ListBox ItemsSource="{Binding Source={StaticResource MyData},XPath=/Contact/Item}" Height="52" Margin="29,22,129,0" Name="listBox1" VerticalAlignment="Top" SelectionChanged="listBox1_SelectionChanged">
                    <TextBlock Text="{Binding XPath=@Name}" />                    
        <TextBox Margin="29,111,129,128" Name="textBox1"/>            

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.