Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

WPF binding case/switch statement?

Posted on 2009-02-17
1
Medium Priority
?
1,562 Views
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?
0
Comment
Question by:zkeown
1 Comment
 
LVL 5

Accepted Solution

by:
rendaduiyan earned 2000 total points
ID: 23666689
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";
                }
            }
        }
<Grid>
        <Grid.Resources>
            <XmlDataProvider x:Key="MyData" XPath="/Contact">
                <x:XData>
                    <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"/>
                    </Contact>
                </x:XData>
            </XmlDataProvider>            
        </Grid.Resources>
        <ListBox ItemsSource="{Binding Source={StaticResource MyData},XPath=/Contact/Item}" Height="52" Margin="29,22,129,0" Name="listBox1" VerticalAlignment="Top" SelectionChanged="listBox1_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding XPath=@Name}" />                    
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBox Margin="29,111,129,128" Name="textBox1"/>            
    </Grid>

Open in new window

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

564 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