1. 程式人生 > >WPF Combobox數據綁定Binding

WPF Combobox數據綁定Binding

ima 根據 esp main startup sel pat aps nta

combobox數據綁定List鏈表集合區分顯示值與選擇的值

整體效果:

技術分享圖片

根據combobox選擇情況分別打印選取值與顯示值

代碼:

Windows窗體:

技術分享圖片
 1 <Window x:Class="ComboxBinding.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="ComBox綁定" Height="192.857"
Width="385" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"> 5 <Grid> 6 <ComboBox Name="comBox1" HorizontalAlignment="Left" Margin="74,10,0,0" Width="209" Height="22" VerticalAlignment="Top"/> 7 <TextBlock Name="txtSelectedValue" Width="200" Text="{Binding ElementName=comBox1, Path=SelectedValue}
" HorizontalAlignment="Left" Margin="115,58,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFE7FBFA"/> 8 <TextBlock Name="txtSelectedText" Width="200" Text="{Binding ElementName=comBox1, Path=Text}" HorizontalAlignment="Left" Margin="114,88,0,0" TextWrapping="Wrap" VerticalAlignment
="Top" Background="#FFE7FBFA"/> 9 <Label Content="selectedValue" HorizontalAlignment="Left" Margin="2,58,0,0" VerticalAlignment="Top"/> 10 <Label Content="selectedText" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top"/> 11 12 </Grid> 13 </Window>
Xaml 窗體cs代碼 技術分享圖片
 1 using System.Collections.Generic;
 2 using System.Windows;
 3 
 4 namespace ComboxBinding
 5 {
 6     /// <summary>
 7     /// MainWindow.xaml 的交互邏輯
 8     /// </summary>
 9     public partial class MainWindow : Window
10     {
11         public MainWindow()
12         {
13             InitializeComponent();
14         }
15 
16         private void Window_Loaded(object sender, RoutedEventArgs e)
17         {
18             List<ComboxBind> lstCmbBind = new List<ComboxBind>();//用於綁定數據源
19 
20             //初始化數據源
21             ComboxBind cbb = new ComboxBind("顯示值1", "選取值1");
22             lstCmbBind.Add(cbb);
23             cbb = new ComboxBind("顯示值2", "選取值2");
24             lstCmbBind.Add(cbb);
25             cbb = new ComboxBind("顯示值3", "選取值3");
26             lstCmbBind.Add(cbb);
27 
28             this.comBox1.ItemsSource = lstCmbBind;
29             comBox1.DisplayMemberPath = "CmbText";//類ComboxBind中的屬性
30             comBox1.SelectedValuePath = "CmbValue";//類ComboxBind中的屬性
31         }
32     }
33 }
View Code

用於綁定combobox的類

技術分享圖片
 1 namespace ComboxBinding
 2 {
 3     /// <summary>
 4     /// 用於Combox數據綁定
 5     /// </summary>
 6     class ComboxBind
 7     {
 8         //構造函數
 9         public ComboxBind(string _cmbText, string _cmbValue)
10         {
11             this.cmbText = _cmbText;
12             this.cmbValue = _cmbValue;
13         }
14 
15         //用於顯示值
16         private string cmbText;
17         public string CmbText
18         {
19             get { return cmbText; }
20             set { cmbText = value; }
21         }
22 
23         //用於實際選取的值
24         private string cmbValue;
25         public string CmbValue
26         {
27             get { return cmbValue; }
28             set { cmbValue = value; }
29         }
30     }
31 }
C#

WPF Combobox數據綁定Binding