Click here to Skip to main content
12,556,918 members (51,851 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XML WPF LINQ C#4.0 combobox , +
Hello friends,

I want to extract the name property from selected item and pass it to a Elements(Xname) property,but don`t know how to convert it.Actually iam working on a filter consisting of two combo boxes and a texbox. Combobox1 is populated with selectedItem`s element(from xmldata) and combobox2 is contains selecteditem`s(xmldata) attribute.

                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" x:Name="stc" >
                        <TextBlock Text="{Binding Path=Name}" Margin="0,0,3,0" x:Name="abc"/>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox1" ItemsSource="{Binding}">
                                    <TextBlock Text="{Binding Path=Name}"/>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox2" ItemsSource="{Binding XPath=@*}">
                                    <TextBlock  Text="{Binding Path=Name}"/>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox3">
                            <ComboBoxItem Content="Equals"/>
                            <ComboBoxItem Content="Greater Than"/>
                            <ComboBoxItem Content="Less Than"/>
                        <TextBox Margin="0,0,4,0" Width="50" x:Name="textbox2" />
                        <Button x:Name="but1" Height="25" Width="35" Click="click" Content="gen" />

The C# Code

 private void click(object sender, RoutedEventArgs e)
        var button = sender as Button;
        var parent = button.Parent as FrameworkElement;
        var attributebox = parent.FindName("cbox2") as ComboBox;
        var textbl = parent.FindName("abc") as TextBlock;
        var elementbox = parent.FindName("cbox1") as ComboBox;
        var combobx = parent.FindName("cbox3") as ComboBox;
        var textBox1 = parent.FindName("textbox2") as TextBox;
        XElement ele = XElement.Load(txtFileName.Text);
        var fil = from item in ele.DescendantsAndSelf(textbl.Text).Elements(elementbox.SelectedItem.ToString())
                  select item;//Attribute(attributebox.SelectedItem.ToString()).Value;
        foreach (var f in fil)
            Label lb = new Label();
            lb.Content = f;
    catch(Exception ex)
        MessageBox.Show(ex.Message,"Invalid value");

All I want is to convert the selected combobox item to string,As i know the selectedItem properties contains object, and i want to extract the name property from the and as i cannot convert the whole object to a please suggest me a way......
Posted 25-Feb-13 5:33am
Updated 25-Feb-13 5:38am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


you have to cast the elementbox.SelectedItem to the appropiate type and then you can access the Name property of it.

But as you didn't provide to what the ComboBox' ItemSource is bound and no information what type of objects this binding contains, I cannot tell you more.

Hope this helps,


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161026.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100