LookUpEdit綁定實體類困擾了我很久,明明和工作室萬哥的前台代碼是差不多的,但是怎麼也綁不成功。後來經過老師的幫忙調試,才找到問題,而這個源頭也讓我十分吐血,居然是GridControl的Name屬性值設的不對,必須要設Name=PART_GridControl,想了半天還是沒有想明白。現附上代碼
首先是Xaml
<dxg:LookUpEdit Height="25" Name="txtSupplierId" DisplayMember="id" AutoPopulateColumns="False" AutoComplete="True" IncrementalFiltering="True" ImmediatePopup="True" IsPopupAutoWidth="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsTextEditable="False" SelectedIndex="-1" PopupOpening="chooseSupplierId"> <dxg:LookUpEdit.PopupContentTemplate> <ControlTemplate> <dxg:GridControl Name="PART_GridControl" ItemsSource="{Binding}"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="編號" Binding="{Binding id}"/> <dxg:GridColumn FieldName="供應商名稱" Binding="{Binding csuppliername}"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView ShowGroupPanel="False"/> </dxg:GridControl.View> </dxg:GridControl> </ControlTemplate> </dxg:LookUpEdit.PopupContentTemplate> </dxg:LookUpEdit>
接下來是綁定後台實體類:
//選擇供應商編號 private void chooseSupplierId(object sender, RoutedEventArgs e) { List<SupplierData> data = new List<SupplierData>(); SupplierData a = newSupplierData(); a.id="1"; a.csuppliername="A" data.Add(a); txtSupplierId.ItemsSource = data;//綁定數據源 } class SupplierData { public string id { get; set; } public string csuppliername { get; set; }//供應商名稱 public string csupplieraddress { get; set; }//供應商地址 }
這樣便可以成功的綁定了~