公共类DA_ActivityType { public int Id {get;组; } public string Name {get;组; } }
public static List<DA_ActivityType> GetActivitytypes()
{
DataContext dbo = new DataContext();
IEnumerable<DA_ActivityType> activityTypes =
from actType in dbo.ActivityTypes
select new DA_ActivityType
{
Id = actType.TypeId,
Name = actType.Name
};
return activityTypes.ToList();
}
// XAML代码
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="ActivityTypes" ObjectType="{x:Type c:ActivityType}" MethodName="GetActivitytypes" />
</Grid.Resources>
<DataGrid AllowDrop="False" AutoGenerateColumns="False" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Margin="1,2,-1,-2" Name="DefineManulaColumns" SelectionMode="Single" VerticalAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ActivityName}" Header="Activity" />
<DataGridTextColumn Binding="{Binding Path=CreatedTime, StringFormat=\{0:D\}}" Header="Created" ClipboardContentBinding="{Binding}" />
<DataGridTextColumn Binding="{Binding Path=StartTime, StringFormat=\{0:f\}}" Header="Start" />
<DataGridTextColumn Binding="{Binding EndTime}" Header="End" />
<DataGridCheckBoxColumn Binding="{Binding Path=Archived}" Header="Archived"/>
<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ActivityName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding ActivityName}" DisplayMemberPath="{Binding Name}" ItemsSource="{Binding Source={StaticResource ActivityTypes}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.AlternatingRowBackground>
<LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
<GradientStop Color="#FFC4DCFF" Offset="0" />
<GradientStop Color="#FFEFF4F8" Offset="1" />
<GradientStop Color="#FF79B7F8" Offset="0.558" />
</LinearGradientBrush>
</DataGrid.AlternatingRowBackground>
</DataGrid>
</Grid>
答案 0 :(得分:1)
或者:
<ComboBox x:Name="myCombo" />
myCombo.ItemsSource = GetMyCollection();
或:
<ComboBox ItemsSource="{Binding}" />
and one of the parent elements must have the DataContext = GetMyCollection();