0

The selected item of my ComboBox only show the path of my item not the content.

How can I fix that?

Here is the code:

ViewModel

class GrundViewModel : BaseViewModel
{
    private ObservableCollection<GrundbildModel> _people;

    public ObservableCollection<GrundbildModel> People
    {
        get { return _people; }
        set
        {
            _people = value;
            OnPropertyChanged(nameof(People));
        }
    }

    public GrundViewModel()
    {
        BtnJOGCommand = new RelayCommand(BtnJOGClick);
        BtnVARCommand = new RelayCommand(BtnVARClick);    

        People = new ObservableCollection<GrundbildModel>();
        People.Add(new GrundbildModel { Value = "John"});
        People.Add(new GrundbildModel { Value = "Jane"});
        People.Add(new GrundbildModel { Value = "Bob"});
    }   

    private GrundbildModel _selectedPerson;

    public GrundbildModel SelectedPerson
    {
        get { return _selectedPerson; }
        set
        {
            _selectedPerson = value;
            OnPropertyChanged(nameof(SelectedPerson));
        }
    } 
    // ....
}

Model:

class GrundbildModel 
{
    public string Value { get; set; }     
}

View:

<ComboBox Name="Combo" Grid.ColumnSpan="6" HorizontalAlignment="Left" VerticalAlignment="Top" Width="522" Height="77" Panel.ZIndex="2"  
temsSource="{Binding People}" DisplayMemberPath="Value" SelectedItem="{Binding SelectedPerson}" />

You can see the combobox here:

enter image description here

Here is the style of my custom control combobox:

 <Style TargetType="{x:Type local:IdxComboBox}">
        <Setter Property="Background" Value="{Binding Path=IdxBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Property="Foreground" Value="{Binding Path=IdxForegroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Property="BorderBrush" Value="{Binding Path=IdxBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Property="FontFamily" Value="{Binding Path=IdxFontFamily, RelativeSource={RelativeSource TemplatedParent}}"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type local:IdxComboBox}">
                    <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Popup x:Name="PART_Popup" AllowsTransparency="true"  Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" VerticalOffset="{TemplateBinding ActualHeight}" Placement="Bottom">
                            <Border x:Name="dropDownBorder" BorderBrush="{TemplateBinding Foreground}" BorderThickness="0" Background="{TemplateBinding Background}" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                                <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                    <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" TextBlock.TextAlignment="Left" TextBlock.FontFamily="{Binding Path=IdxFontFamily, RelativeSource={RelativeSource TemplatedParent}}" TextBlock.FontSize="{Binding Path=IdxFontSize, RelativeSource={RelativeSource TemplatedParent}}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="0,1,0,0">
                                    </ItemsPresenter>
                                </Grid>
                            </Border>
                        </Popup>
                        
                        <ToggleButton x:Name="toggleButton"  Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" >
                            <ToggleButton.Style>
                                <Style TargetType="{x:Type ToggleButton}">
                                    <Setter Property="Background" Value="{Binding Path=IdxForegroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <Setter Property="Foreground" Value="{Binding Path=IdxBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <Setter Property="BorderBrush" Value="{Binding Path=IdxForegroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                                    <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Center" Margin="0" SnapsToDevicePixels="True" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                                        
                                                    </Border>
                                                </Border>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ToggleButton.Style>
                        </ToggleButton>
                        <DockPanel  Grid.ColumnSpan="2" TextBlock.TextAlignment="Center" HorizontalAlignment="Center">
                            <ContentPresenter x:Name="contentPresenter" TextBlock.TextAlignment="Right" TextBlock.FontFamily="{Binding Path=IdxFontFamily, RelativeSource={RelativeSource TemplatedParent}}" TextBlock.FontSize="{Binding Path=IdxFontSize, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="Right" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" >
                            </ContentPresenter>
                            <Polygon x:Name="arrow" Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" Points="0,-2 7,0 5,-7" VerticalAlignment="Center" Margin="0,15,0,0"/>
                        </DockPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        
    </Style>

<Style TargetType="{x:Type local:IdxComboBoxItem}">
    <Setter Property="Background" Value="{Binding Path=IdxBackgroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Foreground" Value="{Binding Path=IdxForegroundColor, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="FontSize" Value="{Binding Path=IdxFontSize, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="FontFamily" Value="{Binding Path=IdxFontFamily, RelativeSource={RelativeSource TemplatedParent}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}" >
                <Border Background="{Binding Path=IdxForegroundColor, RelativeSource={RelativeSource TemplatedParent}}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Margin="{TemplateBinding Margin}"
                        Padding="5"
                        x:Name="myBorder">
                    <Grid>
                        <ContentPresenter Margin="0,1,0,0"
                                          VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                                          TextBlock.FontFamily="{Binding Path=IdxFontFamily, RelativeSource={RelativeSource TemplatedParent}}"
                                          TextBlock.FontSize="{Binding Path=IdxFontSize, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <local:IdxLine Width="auto" VerticalAlignment="Top" Margin="0,-6,0,0"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <!---  <Setter Property="Opacity" Value="0.5"/> -->
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
</Style>

A standard custombox works but not my control. I hope anyone can help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tim W.
  • 1
  • 1
  • `SelectedValuePath="Value"` is wrong. Use only `SelectedItem="{Binding SelectedItemValue}"`. Make sure the SelectedItemValue property setter calls OnPropertyChanged. – Clemens Apr 26 '23 at 08:16
  • Consider using better class names. GrundViewModel and GrundbildModel are horrible. – Clemens Apr 26 '23 at 08:17
  • I changed it but it dont works, same result. – Tim W. Apr 26 '23 at 08:34
  • Then you must have done something wrong that you are not showing here. The current code in your question works as expected. – Clemens Apr 26 '23 at 08:41
  • I use not the standart combobox, i use a customcontroll combobox but where can be the mistake in my customcontrol? – Tim W. Apr 26 '23 at 11:08
  • In the ControlTemplate. – Clemens Apr 26 '23 at 11:41
  • In which ControlTemplate? The combobox use three of them (one for the togglebutton, one for textbox and one for the ComboBoxTemplate itself). – Tim W. Apr 26 '23 at 12:07
  • Thx for help. But te solution was an other. The ContentTemplate of my ContentP resenter and the ContentTemplateSelector were missing. – Tim W. Apr 26 '23 at 12:38

0 Answers0