This sample code is another example on Memeber Binding, where one contol is bound to another. This example contains a C# code and XAML code.
C# code for selecting ComboBox item into ListBox:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data.SqlClient; using System.Data;
namespace WpfApplication1_grid { /// /// Interaction logic for combo2List.xaml /// public partial class combo2List : System.Windows.Window { public combo2List() { InitializeComponent(); comboBox1.SelectionChanged = new SelectionChangedEventHandler( comboBox1_SelectionChanged); }
void comboBox1_SelectionChanged(ob ject sender, SelectionChangedEventArgs e) { //throw new NotImplementedException(); string strItem = ""; System.Data.DataRowView dr = (System.Data.DataRowView)comb oBox1.SelectedItem; strItem = dr.Row["firstname"].ToString(); listBox1.Items.Add(strItem);
} private void Window_Loaded(object sender, RoutedEventArgs e) { ListCat(); }
private void ListCat() { DataSet ds = new DataSet(); SqlConnection con = new SqlConnection("Data source=cp960sw;initial catalog=VH-ENH-POC;uid=sa;pas sword=rmsindia;"); SqlDataAdapter da = new SqlDataAdapter("Select * from student", con); da.Fill(ds); comboBox1.DataContext = ds.Tables[0].DefaultView; comboBox1.DisplayMemberPath = ds.Tables[0].Columns[1].ToString(); comboBox1.SelectedValuePath = ds.Tables[0].Columns[0].ToString(); } } }
XAML code for selecting ComboBox item into Listbox:
<Window x:Class="WpfApplication1_grid .combo2List" Really Long Link Really Long Link Title="combo2List" Height="300" Width="300" Loaded="Window_Loaded"> <Grid> <ComboBox Height="23" Margin="104,43,54,0" ItemsSource="{Binding}" Name="comboBox1" VerticalAlignment="Top" IsSynchronizedWithCurrentItem ="False"></ComboBox> <ListBox Margin="102,121,56,41" Name="listBox1" IsSynchronizedWithCurrentItem ="False" /> </Grid> </Window>
|
No responses found. Be the first to respond and make money from revenue sharing program.
|