2010-04-08 77 views
0

public class DA_ActivityType public int Id {get;組; } public string Name {get;組; }} WPF如何在數據網格中填充組合框

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

你問如何到這個列表綁定到組合框是存在於你的數據網格中? – Amsakanna 2010-04-08 09:50:45

回答

1

或者:

<ComboBox x:Name="myCombo" /> 
myCombo.ItemsSource = GetMyCollection(); 

或:

<ComboBox ItemsSource="{Binding}" /> 
and one of the parent elements must have the DataContext = GetMyCollection();