在DataGrid的每一行中组合框

时间:2016-01-29 09:05:40

标签: c# wpf mvvm datagrid

我正在制作一个与SSIS相同的WPF应用程序,在我的下一步中,我希望将我的源列Mapp到数据库表列。 我希望实现这样的目标 enter image description here

任何人都可以帮我找到一种方法将组合框绑定到数据网格。 我正在使用MVVM设计模式。

2 个答案:

答案 0 :(得分:1)

当您在ComboBox点击鼠标时,可以看到DataGridComboBoxColumn。我们来看一个例子:

<强>型号:

public class Person
{
    public int IdPerson { get; set; }
    public string Name { get; set; }
}

public class Team
{
    public int IdTeam { get; set; }
    public string NameTeam { get; set; }
}

<强>视图模型:

public class MainWindowVM
{
    public MainWindowVM()
    {
        LoadData();
    }

    private void LoadData()
    {
        Persons = new ObservableCollection<Person>() {
        new Person() { IdPerson = 1, Name = "Billy" },
        new Person() { IdPerson = 2, Name = "Bobby" },
        new Person() { IdPerson = 2, Name = "Bond" } };

        Teams = new ObservableCollection<Team>() {
        new Team() { IdTeam = 1, NameTeam = "Team A" },
        new Team() { IdTeam = 2, NameTeam = "Team B" },
        new Team() { IdTeam = 3, NameTeam = "Team C" } };
    }


    public ObservableCollection<Person> Persons { get; set; }
    public ObservableCollection<Team> Teams { get; set; }
}

}

查看:

<Window x:Class="WPFApplication.MainWindow"
        ...the code omitted for the brevity
        xmlns:vm="clr-namespace:WPFApplication.ViewModel"
        Title="a" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <vm:MainWindowVM/>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name}" />

                    <DataGridComboBoxColumn Header="CourtType"
                                            DisplayMemberPath="NameTeam" SelectedValueBinding="{Binding IdTeam}" SelectedValuePath="{Binding IdTeam}"
                                            >
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Teams}"/>
                                <Setter Property="IsReadOnly" Value="True"/>
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Teams}"/>
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>

                    <DataGridTextColumn Binding="{Binding Name}"/>
                </DataGrid.Columns>                
            </DataGrid>
        </StackPanel>
    </Grid>
</Window>

答案 1 :(得分:0)

试试这个:

<DataGrid ItemsSource="{Binding CollectionForDatagrid}">
    <DataGrid.Columns>
         <DataGridTemplateColumn Header="InputColumn">
             <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <ComboBox ItemsSource="{Binding CollectionForComboBox}" />
                  </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>
    <DataGrid.Columns>
</DataGrid>