WPF - example with DataGridComboBoxColumn

限于喜欢 提交于 2019-12-04 01:43:58
Aran Mulholland

the columns in the datagrid dont have a datacontext, as they are never added to the visual tree. sound a bit wierd but have a look at vinces blog, its got a good example of the visual layout. once the grid is drawn the cells have a data context and you can set the combo boxes items source in them using normal bindings (not static resources..)

you can access the combo box items source as such

   <dg:DataGridComboBoxColumn>
      <dg:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=MyBindingPath}" />
        </Style>
      </dg:DataGridComboBoxColumn.EditingElementStyle>
   </dg:DataGridComboBoxColumn>

have a look here and also here for some code

Instead of using DataGridTextColumns one uses a DataGridComboBoxColumn instead. Then one fills in the data using the ItemsSource, which in the below example points to an external enum in the static resource, and finally one binds the result to the target object which will hold the user selection in the SelectedItemBinding.

<DataGrid.Columns>

<DataGridComboBoxColumn Header="MySelections"  
                        SelectedItemBinding="{Binding MySelectionsProperty}" 
                        ItemsSource="{Binding Source={StaticResource mySelectionsEnum}}" />
</DataGrid.Columns>

See a full example on MSDN at DataGridComboBoxColumn Class

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