Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
 
 <ListView.ItemTemplate>
                <DataTemplate>
                    <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}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox2" ItemsSource="{Binding XPath=@*}">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock  Text="{Binding Path=Name}"/>
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>
                        <ComboBox Margin="0,0,3,0" x:Name="cbox3">
                            <ComboBoxItem Content="Equals"/>
                            <ComboBoxItem Content="Greater Than"/>
                            <ComboBoxItem Content="Less Than"/>
                        </ComboBox>
                        <TextBox Margin="0,0,4,0" Width="50" x:Name="textbox2" />
                        <Button x:Name="but1" Height="25" Width="35" Click="click" Content="gen" />
    </StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
 

The C# Code
 
 
         private void click(object sender, RoutedEventArgs e)
        {
            try
            {
                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;
                    canvas1.Children.Add(lb);
                }
            }
            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 string....so please suggest me a way......
Posted 25-Feb-13 6:33am
Edited 25-Feb-13 6:38am
v2

1 solution

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

Solution 1

Hi,
 
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,
 
Thomas.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
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