Using a databound combobox on a datagrid

こ雲淡風輕ζ 提交于 2019-12-24 10:36:37

问题


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:


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.



来源:https://stackoverflow.com/questions/4639264/using-a-databound-combobox-on-a-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!