前台代码:
<Window x:Class="WpfApplication16.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication16"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="sexs" ObjectType="{x:Type local:stuControl}" MethodName="getStuSex" >
</ObjectDataProvider>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="52,48,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="238" CanUserAddRows="False" RowEditEnding="dataGrid1_RowEditEnding">
<DataGrid.Columns>
<DataGridTemplateColumn Header="性别">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate >
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=sexs}}" SelectedItem="{Binding Path=sex}" IsSynchronizedWithCurrentItem="False"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<Label Content="{Binding Path=sex}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
后台代码:
namespace WpfApplication16
{
public class stuControl
{
private static studentDataContext student = new studentDataContext();
public static IQueryable<bool?> getStuSex()
{
var query = from stu in student.stu
select stu.sex;
return query;
}
}
}