如何从包含xml数据的selectedItem中提取name属性并将其转换为字符串。 [英] How to extract the name property from a selectedItem containing xml data and convert it into string.

查看:146
本文介绍了如何从包含xml数据的selectedItem中提取name属性并将其转换为字符串。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



我想从所选项目中提取名称属性并将其传递给Elements(Xname)属性,但不知道如何转换它。实际上我正在使用一个由两个组合框和一个texbox组成的过滤器。 Combobox1填充了selectedItem的元素(来自xmldata),combobox2包含selecteditem`s(xmldata)属性。



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>







C#代码






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");
    }
}





我想要的是将选定的组合框项目转换为字符串,我知道selectedItem属性包含对象,我想从中提取name属性,因为我无法将整个对象转换为字符串....所以请建议我一个方法......



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

推荐答案





你必须施放 elementbox.SelectedItem 到适当的类型,然后你可以访问它的名称属性。



但是你没有不提供ComboBox''ItemSource绑定的内容,也没有提供此绑定包含的对象类型的信息,我不能告诉你更多。



希望这有帮助,



Thomas。
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.


这篇关于如何从包含xml数据的selectedItem中提取name属性并将其转换为字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆