生活随笔
收集整理的這篇文章主要介紹了
18.DataGrid内绑定ComboBox和ListBox以及取值
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
??????? 本章主要解決如何在DataGrid的行內(nèi)綁定ComboBox和ListBox。在數(shù)據(jù)集方面,先建立一個(gè)城市實(shí)體類,這個(gè)實(shí)體類有3個(gè)屬性,分別是城 市名、城市區(qū)號(hào)、城市區(qū)縣集合。城市區(qū)縣集合是很多個(gè)區(qū)縣的集合,所以區(qū)縣類也是一個(gè)實(shí)體類包括2個(gè)屬性分別為區(qū)縣名和區(qū)縣值。在這里城市類集合綁定到 DataGrid中,區(qū)縣類集合綁定到ComboBox和ListBox中。
??????? 首先我們建立城市實(shí)體類和區(qū)縣實(shí)體類集合:
?
///?<summary>????///?城市實(shí)體類????///?</summary>????public?class?City????{????????private?string?cityName;????????private?string?cityNum;????????private?List<Combo>?comboboxList;????????///?<summary>????????///?城市名????????///?</summary>????????public?string?CityName????????{????????????get?{?return?cityName;?}????????????set?{?cityName?=?value;?}????????}????????///?<summary>????????///?城市電話區(qū)號(hào)????????///?</summary>????????public?string?CityNum????????{????????????get?{?return?cityNum;?}????????????set?{?cityNum?=?value;?}????????}????????///?<summary>????????///?城市區(qū)縣類集合????????///?</summary>????????public?List<Combo>?ComboboxList????????{????????????get?{?return?comboboxList;?}????????????set?{?comboboxList?=?value;?}????????}????}????///?<summary>????///?ComboBox需要綁定的類????///?</summary>????public?class?Combo????{????????private?string?name;????????private?string?value;????????///?<summary>????????///?區(qū)縣名????????///?</summary>????????public?string?Name????????{????????????get?{?return?name;?}????????????set?{?name?=?value;?}????????}????????///?<summary>????????///?區(qū)縣值????????///?</summary>????????public?string?Value????????{????????????get?{?return?this.value;?}????????????set?{?this.value?=?value;?}????????}????}? ??????? 然后我們在初始化城市類的集合List<>代碼如下:
?
//實(shí)例化City類集合?List<City>?cityList?=?new?List<City>()?{?new?City()?{?CityName="成都",?CityNum="028",?ComboboxList=new?List<Combo>()?{?new?Combo(){?Name="武侯區(qū)",?Value="28"},?new?Combo(){?Name="青羊區(qū)",?Value="281"},?new?Combo(){?Name="成華區(qū)",?Value="283"},?new?Combo(){?Name="高新區(qū)",?Value="282"},?new?Combo(){?Name="金牛區(qū)",?Value="284"}??}?},?new?City()?{?CityName="北京",?CityNum="010",?ComboboxList=new?List<Combo>()?{?new?Combo(){?Name="朝陽區(qū)",?Value="10"},?new?Combo(){?Name="海淀區(qū)",?Value="103"},?new?Combo(){?Name="崇文區(qū)",?Value="104"},?new?Combo(){?Name="豐臺(tái)區(qū)",?Value="105"},?new?Combo(){?Name="東城區(qū)",?Value="120"}??}?}?};? ???????? 最后? this.ShowCityList.ItemsSource = cityList;將城市類集合綁定到DataGrid的ItemsSource上面。下面我們來觀看DataGrid的XAML代碼,在這里主要是在 DataGrid的DataGridTemplateColumn.CellTemplate模板下面添加DataTemplate數(shù)據(jù)模板,在這個(gè)模板 下面添加一個(gè)ComboBox或者ListBox控件,ComboBox和ListBox的ItemsSource綁定區(qū)縣類集合的 ComboboxList屬性(?ItemsSource="{Binding ComboboxList}")。當(dāng)然這樣綁定下來顯示的名稱是不正確的。所以ComboBox控件還需要添加 ComboBox.ItemTemplate模板,此模板內(nèi)部在綁定一個(gè)TextBlock控件,此控件的Text屬性綁定區(qū)縣類的Name屬性 (Text="{Binding Name}")。
?
<sdk:DataGrid?HorizontalAlignment="Left"?AutoGenerateColumns="False"?Margin="28,71,0,0"?Name="ShowCityList"?VerticalAlignment="Top"?Height="271"?Width="324"?>?<sdk:DataGrid.Columns>?<sdk:DataGridTextColumn?Header="城市"?Binding="{Binding?CityName}"?IsReadOnly="True"?Width="108"/>?<sdk:DataGridTemplateColumn?Header="區(qū)縣">?<sdk:DataGridTemplateColumn.CellTemplate>?<DataTemplate>?<ComboBox?Width="80"?Height="24"?ItemsSource="{Binding?ComboboxList}"?SelectionChanged="ComboBox_SelectionChanged">?<ComboBox.ItemTemplate>?<DataTemplate>?<TextBlock?Width="80"?Text="{Binding?Name}"?></TextBlock>?</DataTemplate>?</ComboBox.ItemTemplate>?</ComboBox>?</DataTemplate>?</sdk:DataGridTemplateColumn.CellTemplate>?</sdk:DataGridTemplateColumn>?<sdk:DataGridTemplateColumn?Header="區(qū)縣級(jí)別">?<sdk:DataGridTemplateColumn.CellTemplate>?<DataTemplate>?<ListBox?Width="80"?ItemsSource="{Binding?ComboboxList}"?>?<ListBox.ItemTemplate>?<DataTemplate>?<TextBlock?Width="80"?Text="{Binding?Name}"></TextBlock>?</DataTemplate>?</ListBox.ItemTemplate>?</ListBox>?</DataTemplate>?</sdk:DataGridTemplateColumn.CellTemplate>?</sdk:DataGridTemplateColumn>?</sdk:DataGrid.Columns>?</sdk:DataGrid>? ??????? 在ComboBox中,我添加了一個(gè)事件SelectionChanged="ComboBox_SelectionChanged"來捕捉當(dāng) ComboBox改變選項(xiàng)之后獲取ComboBox的值。下面請看ComboBox_SelectionChanged的事件處理代碼:
?
private?void?ComboBox_SelectionChanged(object?sender,?SelectionChangedEventArgs?e)?{?ComboBox?combobox?=?sender?as?ComboBox;?//選擇到的項(xiàng)轉(zhuǎn)化為類Combo。?Combo?combo?=?combobox.SelectedValue?as?Combo;?MessageBox.Show(combo.Name+"的區(qū)號(hào)是:"+combo.Value);?}? ??????? 注意:使用ComboBox.SelectedValue獲取到的是實(shí)體類Combo。
??????? 本例采用VS2010+Silverlight4.0編寫,點(diǎn)擊?SLDataTemplate.rar 下載實(shí)例源碼,下面大家看效果圖:
?
轉(zhuǎn)載于:https://blog.51cto.com/chengxingliang/821957
總結(jié)
以上是生活随笔為你收集整理的18.DataGrid内绑定ComboBox和ListBox以及取值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。