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; }//供应商地址
}
这样便可以成功的绑定了~