DataGridComboBoxColumn cell not displaying selected item text?

左心房为你撑大大i 提交于 2019-11-29 14:48:06

This was the only resource on DataGridComboBoxColumn that I found helpful:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx

Everything else (that I found on non-MSDN sites) was misleading!

Ok I figured it out after a lot of googling.

but it seems you just do the same thing for ElementStyle, again with the target type of combo box, even though it does not seem to show a ComboBox when not editing.

<DataGridComboBoxColumn Header="Formatter" SelectedItemBinding="{Binding Path=Format}">
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
                <Setter Property="IsDropDownOpen" Value="True" />
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding Path=DefinedFormatters}" />
                <Setter Property="IsDropDownOpen" Value="True" />
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}"></TextBlock>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>

I'm quite at a loss, but maybe try the following: Remove the ElementStyle and instead set the DisplayMemberPath, e.g.:

<DataGridComboBoxColumn Header="Formatter" SelectedItemBinding="{Binding Path=Format}" DisplayMemberPath="Name">

Path might also be FormatView.Name, I don't have a clear picture of your data structure. And how did you set the ItemsSource of your DataGridComboBoxColumn?

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