DisplayMemberPath doesn't affect the selected item in text area of the ComboBox?

Hi

I have a ComboBox with a list of objects as the source. So I use DisplayMemberPath to display the "Name" property in the dropdown list.

However, when I select an item in the list it doesn't use the DisplayMemberPath in the text area and it just displays the object it self (as a string).

Best regards,
Thomas Andersen



Answer this question

DisplayMemberPath doesn't affect the selected item in text area of the ComboBox?

  • rahsoftware

    Hi Thomas,

    I can't reproduce what you experienced, here's what I tried:

    <ComboBox DisplayMemberPath="Length" xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String>a</s:String>
    <s:String>aa</s:String>
    <s:String>aaa</s:String>
    </ComboBox>

    I see 1, 2, 3 listed, then when I select an individual item I still see only the corresponding number.


  • deulu

    I found it. I missed:
    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

    Thanks for your help.

    Best regards,
    Thomas Andersen


  • Bigmo

    Don't have a chance to try this right now, but I think you might want to set the Content property of the ContentPresenter to just be {Binding}.
  • Antonio Bevilacqua

    Ohh, you are right. I found out it has to do with my ControlTemplate.

    I use something like this (simplyfied):

    <Grid>

    <Grid.Resources
    >

    <
    ControlTemplate x:Key="ComboBoxTemplate" TargetType="ComboBox"
    >
    <
    Grid SnapsToDevicePixels="True"
    >
    <
    Border BorderBrush="{TemplateBinding Border.BorderBrush}"

    BorderThickness="{TemplateBinding Border.BorderThickness}"

    Name="Bd" Background="{TemplateBinding Panel.Background}"

    Padding="1,1,1,1"
    >
    <
    Grid Grid.IsSharedSizeScope="True"
    >
    <
    Grid.ColumnDefinitions
    >
    <
    ColumnDefinition Width="1"
    />
    <
    ColumnDefinition Width="*"
    />
    <
    ColumnDefinition Width="Auto"
    />
    </
    Grid.ColumnDefinitions
    >
    <
    Border Margin="{TemplateBinding Control.Padding}"

    Name="SelectedItemBorder"

    Grid.ColumnSpan="2"
    />
    <
    ContentPresenter Margin="{TemplateBinding Control.Padding}"

    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"

    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"

    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"

    ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"

    Content="{TemplateBinding ComboBox.SelectionBoxItem}"

    Grid.Column="1"
    />
    <
    ToggleButton x:Name="ToggleButton
    "
    Grid.Column="2
    "
    IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
    >
    xx
    </ToggleButton
    >
    </
    Grid
    >
    </
    Border
    >
    <
    Popup Placement="Bottom" Name="PART_Popup" Focusable="False" MinWidth="{TemplateBinding FrameworkElement.ActualWidth}" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}" AllowsTransparency="True" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
    >
    <
    Border BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1,1,1,1" Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
    >
    <
    ScrollViewer
    >
    <
    ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"
    />
    </
    ScrollViewer
    >
    </
    Border
    >
    </
    Popup
    >
    </
    Grid
    >
    </
    ControlTemplate
    >

    <
    Style TargetType="{x:Type ComboBox}"
    >
    <
    Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"
    />
    </
    Style
    >

    </
    Grid.Resources
    >

    <
    ComboBox Height="26" Margin="161,158,224,0" Name="comboBox1" VerticalAlignment="Top" DisplayMemberPath="Length" xmlns:s="clr-namespace:System;assembly=mscorlib"
    >
    <
    s:String>a</s:String
    >
    <
    s:String>aa</s:String
    >
    <
    s:String>aaa</s:String
    >
    </
    ComboBox
    >

    </
    Grid>

    I guess the problem is the ContentPresenter.
    Someone knows if I'm missing a TemplateBinding or do I need to change the Template

    Best regards,
    Thomas Andersen


  • DisplayMemberPath doesn't affect the selected item in text area of the ComboBox?