簡體   English   中英

如何從列表框中的按鈕中提取值?

[英]How to extract values from the button in listbox?

我正在嘗試創建體育新聞應用程序。 我在列表框中創建了一個按鈕,其中顯示了所有新聞,說明等。 沒有 的按鈕取決於xml文件。 當我單擊按鈕時,我想在下一頁上顯示該按鈕的新聞和說明。 我怎么做? 謝謝

<ListBox x:Name="lstShow" FontFamily="Arial Black" VerticalAlignment="Center"
   Margin="-6,0,0,-26" Height="610" RenderTransformOrigin="0.5,0.5"
   Background="{x:Null}" Opacity="0.8">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Button Width="450" Height="Auto" Background="Black" 
                  BorderBrush="Transparent" FontWeight="Bold" FontSize="23" 
                  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,5" 
                  Opacity="0.95" Click="news_click" Foreground="White">
                    <StackPanel>
                        <Image Source="{Binding Imageurl}" Width="200" Height="Auto"
                          HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <TextBlock   TextWrapping="Wrap" FontFamily="Segoe WP Black" 
                          Foreground="White" FontSize="18" HorizontalAlignment="Stretch" 
                          VerticalAlignment="Stretch" TextAlignment="Left" Width="350" 
                          Height="150">
                        <Run FontSize="23" Text="{Binding News}" />
                        <LineBreak/>
                        <Run Text="{Binding Desc}" FontWeight="Normal" FontSize="16" 
                          FontFamily="Segoe WP SemiLight"/>
                        <LineBreak/>
                        <Run Text="{Binding Newsurl}" FontWeight="Normal" FontSize="16" 
                          FontFamily="Segoe WP SemiLight"/>

                     </TextBlock>
                  </StackPanel>
               </Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

背后的代碼:

myData = XDocument.Parse(e.Result, LoadOptions.None);
// var data = myData.Descendants("headlines").FirstOrDefault();
var data1 = from query in myData.Descendants("headlinesItem")
    where query.Element("images").Descendants("imagesItem").Any()
    select new UpdataNews
    {
        News = (string)query.Element("headline").Value,
        Desc = (string)query.Element("description"),
        Newsurl = (string)query.Element("links").Element("mobile").Element("href"),
        Imageurl = (string)query.Element("images").Element("imagesItem").Element("url"),
    };
lstShow.ItemsSource = data1;

有很多方法。

  1. 如果使用MVVM,最好的方法是在按鈕上設置一個Command並將CommandParameter設置為綁定。

    <Button Command = {Binding ClickCommand}“ CommandParameter =” {Binding}“ ...

  2. 如果您不使用MVVM,則可以執行以下操作:

    <Button Tag =“ {Binding}” Click =“ My_Click_Event” ...

然后在后面的代碼中執行以下操作:

public void My_Click_Event(object sender, EventArgs args)
{
  var o = ((FrameworkElement)sender).Tag;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM