Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

HTML
 <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

C#
 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
Updated 25-Feb-13 5:38am
v2

1 solution

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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900