Windows Phone with XML -显示特定的标签

本文关键字:标签 显示 Phone with XML Windows | 更新日期: 2023-09-27 18:16:03

我正在开发一个应用程序,我的"ListBox"显示信息的XML文件,获取信息并显示在"TextBox"。但是我不希望"ListBox"answers"TextBox"显示所有的XML标签。

现在是第一次。第二个应该是:

http://infassteste.url.ph/ats.png

http://infassteste.url.ph/dps.jpg

我的XML:

<produtos>
<produto>
<codigo>1</codigo>
<descricao>Janela</descricao>
<valor>100.00</valor>
</produto>
<produto>
<codigo>2</codigo>
<descricao>Porta</descricao>
<valor>1000.00</valor>
</produto>
<produto>
<codigo>3</codigo>
<descricao>Cimento</descricao>
<valor>10.00</valor>
</produto>
<produto>
<codigo>4</codigo>
<descricao>Arame</descricao>
<valor>60.00</valor>
</produto>
</produtos> 

My Xaml file:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock TextWrapping="Wrap" Text="Quantidade:" Margin="6,235,326,373"/>
        <TextBox MaxLength="3" InputScope="Number" HorizontalAlignment="Left" Height="71" Margin="135,215,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="86" Name="Qtd"/>
        <Button Content="Adicionar" HorizontalAlignment="Left" Margin="279,212,0,0" VerticalAlignment="Top" Width="148" Click="Button_Enviar"/>
        <TextBlock HorizontalAlignment="Left" Margin="10,322,0,0" TextWrapping="Wrap" Text="Lista de Produtos:" VerticalAlignment="Top" Height="30" Width="166"/>
        <ListBox x:Name="listBox1" 
             Height="192" Width="456" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Top" 
             BorderThickness="5"
             Padding="5"
             BorderBrush="Blue"
             Background="Black"
             FontSize="30"
             />
        <TextBox x:Name="txtList" IsReadOnly="True" HorizontalAlignment="Left" Height="267" Margin="0,357,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456"/>
    </Grid>
我代码:

namespace InfamatPocket
{  
public partial class Produtos : PhoneApplicationPage
{

    public Produtos()
    {
        InitializeComponent();
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

        try
        {
            XDocument doc = XDocument.Load(store.OpenFile("im.xml", FileMode.Open, FileAccess.Read));
            this.listBox1.ItemsSource = from query in doc.Descendants("produto") select query.Value;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }


    private void Button_Enviar(object sender, RoutedEventArgs e)
    {
        String dedos = " " + Qtd.Text + " | " + listBox1.SelectedItem;
        String dados = "'nProduto: " + listBox1.SelectedItem + "'nQuantidade: " + Qtd.Text;
        MessageBox.Show(dados);
        if (!string.IsNullOrEmpty(dados))
        {
            txtList.Text += dedos + "'n";
        }
       // this.listBox2.ItemsSource = "Quantidade:" + Qtd.Text + " | " + listBox1.SelectedItem;

    }
}
}

Windows Phone with XML -显示特定的标签

使您的LINQ查询返回具有两个属性的模型,例如descricaovalor(为了简单起见,我在这里使用匿名类型):

listBox1.ItemsSource = from query in doc.Descendants("produto") 
                       select new
                       {
                          descricao = (string)query.Element("descricao"),
                          valor = (string)query.Element("valor")
                       };

ListBox设置<ItemTemplate>,以便ListBox中的每个项目能够以我们想要的方式显示多个值:

<ListBox x:Name="listBox1" 
         .....
         .....
         HorizontalContentAlignment="Stretch"
         >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding descricao}"/>
                <TextBlock Grid.Column="1" Text="{Binding valor}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>