简体   繁体   中英

Data-bound WPF ComboBox not displaying selected value

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM