Home > other >  WPF ListBox template column click events, access to one of the values
WPF ListBox template column click events, access to one of the values

Time:01-01


When beginners write WPF routine template column click event, can only get click event, but unable to get a template column TextBlock values
I just want to when the click event for a certain value
For example I have such a few data
The first name: Chen
The second name: Chen Xiaozhen
Type: None
The first name: north Texas
The hand of the second name: north Texas
Type: Processing
The first name: black roses
The second name: black roses storm
Type: Progress
The first name: zaun
The second name: zaun madman
Type: Menaduo
How to obtain when click Chen this value
Post code below, such as online bosses to help... Thank you for
XMLNS="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
XMLNS: x="http://schemas.microsoft.com/winfx/2006/xaml"
XMLNS: d="http://schemas.microsoft.com/expression/blend/2008"
XMLNS: MC="http://schemas.openxmlformats.org/markup-compatibility/2006"
XMLNS: local="CLR namespace: Wpf_Demo_Test"
MC: Ignorable="d"
Title="binding common controls and access to basic values" Height="300" Width="300" & gt;











<17,8,15,26 ListBox Margin="" Name=" listBox1 ItemsSource "=" {Binding Tables [0]} "
ItemTemplate="{StaticResource listBoxTemplate}" the SelectionChanged="listBox1_SelectionChanged"/& gt;


The background data binding,
using System;
using System.Collections.Generic;
using System.Data;
Using System. The Data. SqlClient.
using System.Linq;
using System.Text;
Using System. The Threading. The Tasks;
Using System. Windows;
Using System. Windows. Controls;
Using System. Windows. The Data;
Using System. Windows. The Documents;
Using System. Windows. Input;
Using System. Windows. Media;
Using System. Windows. Media. Imaging;
Using System. Windows. Shapes;
The namespace Wpf_Demo_Test
{
///& lt; Summary>
///binding common controls and access to basic values. Xaml interaction logic
///& lt;/summary>
Public partial class of commonly used controls binding and obtain basic value: Window
{
Public binding common controls and access to essential value ()
{
InitializeComponent();
BindData ();
}
String SQL="select FirstName, LastName, Enmu from Customer".
String the connectionString="Data Source=. Initial Catalog=, customertest; Integrated Security=True ";
///& lt; Summary>
///when the page loads data binding approach
///& lt;/summary>
Private void BindData ()
{
The DataSet ds=new DataSet ();
Using (SqlConnection SQLCN=new SqlConnection (the connectionString))
{
Using (SqlCommand CMD=new SqlCommand (SQL, SQLCN))
{
SqlDataAdapter adapter=new SqlDataAdapter ();
SQLCN. The Open ();
Adapter. SelectCommand=CMD;
Adapter. The Fill (ds, "province");
ListBox1. DataContext=ds;
}
}
}


//the drop-down listbox selected events
Private void listBox1_SelectionChanged (object sender, SelectionChangedEventArgs e)
{
//object s1=listBox1. SelectedValue;
MessageBox. Show (" we selected the option ");//System. Data. DataRowView
//we are not only to show entity, we should also get selected items of a certain value
}



}
}

CodePudding user response:


Instead of using event, you might as well use winform, such did not play out the advantage of WPF, the WFP MVVM pattern very cow force, a book about WPF will mentioned casually, do not use events, with two-way binding,
XMLNS="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
XMLNS: x="http://schemas.microsoft.com/winfx/2006/xaml"
XMLNS: d="http://schemas.microsoft.com/expression/blend/2008"
XMLNS: MC="http://schemas.openxmlformats.org/markup-compatibility/2006"
XMLNS: local="CLR namespace: WPFTest"
MC: Ignorable="d"
Title="MainWindow" Height="450" Width="800" & gt;




Using System. Windows;

The namespace WPFTest
{
///& lt; Summary>
///MainWindow. Xaml interaction logic
///& lt;/summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent();
Enclosing DataContext=new ViewModel ();
}
}
}

using System.Collections.Generic;
Using System. Windows;

The namespace WPFTest
{
The class ViewModel
{
Public ViewModel ()
{
People=new List nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related