开发者

Data-bound WPF ComboBox not displaying selected value

开发者 https://www.devze.com 2023-04-08 04:59 出处:网络
I have a ComboBox, bound to a DataTable. The ComboBox displays a list of values, pulled from the \"wellId\" column of the DataTable. The ComboBox is also styled so that I can insert a custom item into

I have a ComboBox, bound to a DataTable. The ComboBox displays a list of values, pulled from the "wellId" column of the DataTable. The ComboBox is also styled so that I can insert a custom item into the list simply by adding a dummy row to the DataTable with the wellId field set to "(settings)".

<ComboBox IsEditable="True" Name="comboWell" ItemsSource="{Binding}">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Content" Value="{Binding wellId}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding wellId}" Value="(settings)">
                    <Setter Property="Content" Value="Customize..." />
                </DataTrigger>
            <开发者_运维技巧;/Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

For the most part, this works great. It shows the list, and all items (including the dummy items) are selectable in the drop-down list.

However, after selecting an item from the list, whether it is a real item or a dummy item, the ComboBox doesn't show the selected item properly. Rather than showing the same value displayed in the drop-down list (the "wellId" column from the DataTable), it instead just displays the string "System.Data.DataRowView". No matter what I select, it always displays the same thing.

If I specifically set the DisplayMemberPath on the ComboBox to "wellId", then it displays the selected item properly. However, this messes up all of the other styling I have applied, resulting in the drop-down list being filled with blank entries.

How do I get the ComboBox to display the selected item properly?


Change your ComboBox to set the ItemTemplate instead of the ItemContainerStyle, and remove IsEditable=True. If IsEditable=True then the SelectedItem will get displayed in a TextBox, and if a TextBox.Text is bound to an item, it will display the .ToString() of that item

<ComboBox Name="comboWell" ItemsSource="{Binding }">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentControl>
                <ContentControl.Style>
                    <Style TargetType="{x:Type ContentControl}">
                        <Setter Property="Content" Value="{Binding wellId}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding wellId}" Value="(settings)">
                                <Setter Property="Content" Value="Customize..." />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
0

精彩评论

暂无评论...
验证码 换一张
取 消