首先在添加一个模板列
<DataGrid.Columns> <DataGridTemplateColumn Header="ComboBox模板列" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=SelectedTypeResource}}" DisplayMemberPath="Element[ItemName].Value" Text="{Binding Element[SelectedType].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>
申明一个ComboBox下拉菜单绑定内容的数组
public static XElement[] SelectedTypeResource = new XElement[] { new XElement("Item",new XElement("ItemName","下拉菜单一"),new XElement("ItemCode",1)), new XElement("Item",new XElement("ItemName","下拉菜单二"),new XElement("ItemCode",2)) };
在构造函数中加入本地资源字典里
public MainWindow() { InitializeComponent(); (this.Resources["SelectedTypeResource "] as ObjectDataProvider).ObjectInstance = SelectedTypeResource ; }
在设计器里面
<Window.Resources> <ObjectDataProvider x:Key="SelectedTypeResource "></ObjectDataProvider> </Window.Resources>
原文:http://www.cnblogs.com/wujiaqi-IT/p/4498532.html