[英]How to get value of selected listbox item
I'm loading data from an xml file into a listbox . 我正在将数据从xml文件加载到列表框中。 Here is my xaml
这是我的xaml
<ListBox x:Name="lstSearchCategory" FontFamily="Arial Black"
VerticalAlignment="Center" Margin="25,69,19,10" Height="264">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel >
<Image Source="{Binding Image}" Height="100" Width="100"
HorizontalAlignment="Left"></Image>
<TextBlock HorizontalAlignment="Right" Text="{Binding Name}"
FontSize="30" Foreground="Black" Margin="140,-100,0,0"/>
<TextBlock Text="{Binding Category}" FontSize="24"
Foreground="Black" Margin="10,-10,0,0"/>
<TextBlock Text="{Binding Price}" HorizontalAlignment="Right"
Foreground="Red" Margin="300,-25,0,16"/>
<Rectangle Width="500" Fill="Black" Height="0.5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is working fine. 一切正常。 Now I want that when I select any listbox item, I get its respective values ie image, price, category etc. How can i do this ?
现在我想要当我选择任何列表框项目时,得到其各自的值,即图像,价格,类别等。我该怎么做? Help
救命
You need to get the selected item in a ListBox Event and get the DataTemplate from the ListBox (as seen on MSDN ): 您需要在ListBox事件中获取选定的项,并从ListBox获取DataTemplate(如MSDN所示 ):
private void lstEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem lbi = (lstEvents.ItemContainerGenerator.ContainerFromIndex(lstEvents.SelectedIndex)) as ListBoxItem;
ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(lbi);
DataTemplate dt = lstEvents.ItemTemplate;
Label l = (dt.FindName("lblEventId", cp)) as Label;
MessageBox.Show(l.Content.ToString());
}
You need generate Tap = "lstSearchCategory_Tap"
in your XAML file and below code in .cs
file 您需要在XAML文件中以及
.cs
文件中的以下代码中生成Tap = "lstSearchCategory_Tap"
private void lstSearchCategory_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
try
{
ListBox ListBoxSelecteditem = (ListBox)sender;
YourModel model = (YourModel)ListBoxSelecteditem.SelectedItem;
string name = model.Name;
string cat = model.Category;
.......
string ControlName = ((System.Windows.FrameworkElement)
(((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Name;
if (ControlName.ToLower() != "name".ToLower())
{
}
}
catch (Exception ex)
{ }
}
try this 尝试这个
<ListBox Tap="lstSearchCategory_Tap" x:Name="lstSearchCategory">
and than on tap event add this 并且比点击事件添加此
var selected = (classname)lstSearchCategory.SelectedValue;
MessegeBox.Show(selected.Name + selected.Price);
here class-name is name of class where you are binding the name, price etc values 这里class-name是要绑定名称,价格等值的类的名称
If you fill your ListBox
via binding, you should have some property lile SelectedItem
in your view model. 如果通过绑定填充
ListBox
,则视图模型中应具有一些属性lile SelectedItem
。 So the currently selected item should always be stored in the viewmodel for easy access. 因此,当前选择的项目应始终存储在视图模型中以便于访问。 Just add a binding to
SelectedItem
in your viewmodel and every thing should work. 只需在您的视图模型中向
SelectedItem
添加一个绑定,一切都会正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.