WPF如何在数据网格中填充组合框

Posted

技术标签:

【中文标题】WPF如何在数据网格中填充组合框【英文标题】:WPF how to fill combobox in data grid 【发布时间】:2011-02-05 15:04:48 【问题描述】:

公共类 DA_ActivityType 公共 int ID 获取;放; 公共字符串名称 获取;放;

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>

【问题讨论】:

您是在问如何将此列表绑定到数据网格中存在的组合框? 【参考方案1】:

要么:

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

或:

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

【讨论】:

以上是关于WPF如何在数据网格中填充组合框的主要内容,如果未能解决你的问题,请参考以下文章

读取自定义Datagrid的每个单元格数据 - WPF C#

在 WPF 数据网格、组合框模板列上单击编辑

我需要简单的例子来在数据网格中使用组合框(MVV

将组合框添加到链接到数据表的数据网格

在wpf中访问数据网格内的组合框属性

如何在 WPF 中隐藏组合框的项目