[英]How to extract values from the button in listbox?
I am trying to create a sports news app. 我正在尝试创建体育新闻应用程序。 I've created one button inside the listbox which shows all the News,description,etc.
我在列表框中创建了一个按钮,其中显示了所有新闻,说明等。 There are no.
没有 of buttons depends on xml file.
的按钮取决于xml文件。 When i click on the button i want to show the News and description from that button on the the next page.
当我单击按钮时,我想在下一页上显示该按钮的新闻和说明。 how do i do that?
我怎么做? Thanks
谢谢
<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>
Code Behind: 背后的代码:
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;
There are many ways. 有很多方法。
If you use MVVM, the best way is to set a Command on your button and set the CommandParameter to the binding. 如果使用MVVM,最好的方法是在按钮上设置一个Command并将CommandParameter设置为绑定。
<Button Command={Binding ClickCommand}" CommandParameter="{Binding}" ... <Button Command = {Binding ClickCommand}“ CommandParameter =” {Binding}“ ...
If you don't use MVVM you can do this: 如果您不使用MVVM,则可以执行以下操作:
<Button Tag="{Binding}" Click="My_Click_Event" ... <Button Tag =“ {Binding}” Click =“ My_Click_Event” ...
Then in the code behind just do: 然后在后面的代码中执行以下操作:
public void My_Click_Event(object sender, EventArgs args)
{
var o = ((FrameworkElement)sender).Tag;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.