Using a databound combobox on a datagrid

2019-08-06 10:00发布

With a datagrid, I want to use a databound combobox to set the value of a property with the combobox's selected value. How would I go about doing that?

Cheers

1条回答
男人必须洒脱
2楼-- · 2019-08-06 10:36

This can easily be achieved using the WPF DataGrid's CellTemplate features:

<DataGrid.Columns>
    <DataGridTemplateColumn Header="My Column">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding MyBoundField}"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
        <DataGridTemplateColumn.CellEditingTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding MyOptionsSource}" IsEditable="False"/>
            </DataTemplate>
        <DataGridTemplateColumn.CellEditingTemplate>
    </DataGridTemplateColumn>
<DataGrid.Columns/>

Now just handle the ComboBox SelectionChanged event and force a Commit by giving the DataGrid keyboard focus :)

Have fun.

查看更多
登录 后发表回答