954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

istbox selection problem C#, XaML, XML

Hi,

I have a question regarding navigating to a listbox item in C#. I have written code which populates a listbox from an xml file in xaml (thus bound to it). However I want to navigate to the selected item in the listbox using code in c#. I can navigate through the xml document without issues but how do I call the specific item that is selected in the listbox? For example, the listbox has items populated from the xml at node 1 and node 2 respectively: "Coffee" {i.e node1}, "Cream" {i.e node2}. By selecting 'Coffee', how can I specify this in the C# code?

For more info here is part of the xaml code:

<ListBox x:Name="lbRecipe2" Height="auto" Width="150" BorderBrush="Blue"  IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding ElementName=lbRecipe, Path=SelectedItem, Mode=OneWay}" SelectionChanged="lbRecipe2_SelectionChanged" >
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Height="auto">
                                        <TextBlock FontWeight="Bold" FontSize="12" Text="{Binding XPath=name}" />
                                        <TextBlock FontSize="11" Text="{Binding XPath=summary}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox> 

And here is part of the c# code {note the lbrecipe2. which is incomplete): 

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            if (lbRecipe2.Items.GetItemAt == "")
            {

            }
        }

Any help is greatly appreciated.

Thanks

ibdatx
Light Poster
32 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This is how you get selected item from a listBox:

private void PopulatingListBox()
        {
            string[] ar = new string[] { "Coffee", "Cream" };
            listBox1.Items.AddRange(ar);
        }
 
         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string item = this.listBox1.SelectedItem.ToString();
            MessageBox.Show("Selected item is : " + item);
        }


Hope it helps,

Mitja

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Thanks Mitja, that definitely helped.

However I have another question. I can locate the value of an xml element such as value. How can I locate the attribute value of an element such as ?

Thanks again.

ibdatx
Light Poster
32 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: