[英]WPF How to get values from stackpanel inside a ListBoxItem
I'm currently developing a WPF application that contains a ListBox
and inside it a ListBoxItem
Within a StackPanel
and one Image
and Label
inside StackPanel
. 我目前正在开发一个WPF应用程序,其中包含一个
ListBox
并在其中包含一个StackPanel
的ListBoxItem
和一个在StackPanel
Image
和Label
。 All ListBoxItem are generated through database values and displayed inside ListBox
control. 所有ListBoxItem都是通过数据库值生成的,并显示在
ListBox
控件内。 But when user select one ListBoxItem
how to get a value from a Label
for example? 但是,当用户选择一个
ListBoxItem
时,例如如何从Label
获取值? This is my XAML code: 这是我的XAML代码:
<ListBox Name="LIstBProdutos"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalAlignment="Left"
Height="336"
Margin="39,98,0,0"
VerticalAlignment="Top"
Width="358">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Code Behind Method 方法背后的代码
private void BTTProduct_Click(object sender, RoutedEventArgs e)
{
ListProd.Items.Clear();
SqlDataReader reader;
string consulta = "";
if (CCKBOXFilterProdCat.IsChecked == true)
{
var idCategoria = Int32.Parse(((DataRowView)CBBFilterCatProd.SelectedItem)["id"].ToString());
consulta = "and categoria ='" + idCategoria + "'";
}
c.ConsultaSql("select * from produto where nome like '%" + TBXNomeProduto.Text + "%'" + "" + consulta + " ");
c.NonQuery();
c.DataSet();
reader = c.cm.ExecuteReader();
int nome = 0;
for (int a = 0; a < c.ds.Tables[0].Rows.Count; a++)
{
nome = nome+1;
ListBoxItem lbi = new ListBoxItem();
lbi.Width = 100;
lbi.Height = 152;
Image img = new Image();
StackPanel stp = new StackPanel();
Label lbl = new Label();
Label lbl2 = new Label();
stp.Name = "Stack"+ nome.ToString();
reader.Read();
string IdImagem = reader["id"].ToString();
lbl.Content = reader["nome"].ToString();
lbl2.Content = "R$ " + reader["preco"].ToString();
var pa = System.IO.Path.Combine(Environment.CurrentDirectory, "produtos/");
var uri = new Uri(pa + IdImagem + ".jpg");
BitmapImage bm = new BitmapImage(uri);
img.Source = bm;
stp.Children.Add(img);
stp.Children.Add(lbl);
stp.Children.Add(lbl2);
stp.ToolTip = reader["nome"].ToString() + "\n Somente R$ " + reader["preco"].ToString();
lbi.Content = stp;
ListProd.Items.Add(lbi);
}
}
How to get the value from ListBoxItem
? 如何从
ListBoxItem
获取值?
You can do probably do something like this 你可以做这样的事情
You can create a class with your Database Fields you are fetching 您可以使用要获取的数据库字段创建一个类
public class DbRecord
{
public string ImagePath { get; set; }
public string nome { get; set; }
public string preco { get; set; }
}
Now after checking whether any record is selected in the ListBox you can create the object of the Class we have created and then fetching the Record. 现在,在检查是否在列表框中选择了任何记录之后,您可以创建我们创建的Class对象,然后获取Record。
if (ListProd.SelectedIndex >= 0)
{
DbRecord Record = new DbRecord();
Record = (ListProd.SelectedItem) as DbRecord;
string nome = Record.nome;
string preco = Record.preco;
}
You have the values of both the labels in the variables. 您在变量中都有两个标签的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.