WPF binding case/switch statement?

Posted on 2009-02-17
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?
Question by:zkeown
    1 Comment
    LVL 5

    Accepted Solution

    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


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now