0

I want to build a simple hierarchical Combobox. So i build a converter, that adds a number of spaces before the text, which works fine. However, when an Item is selected, I want to display the text of the SelectedItem without the spaces.

The xaml code:

        <ComboBox ItemsSource="{Binding KategorieAuswahl}" SelectedItem="{Binding Kategorie, Mode=TwoWay}" Grid.Column="1" Grid.Row="2" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Converter={StaticResource iFc}}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

The c# converter:

class vcIndentForComboBox : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value.GetType() == typeof(Kategorie))
        {
            Kategorie k = (Kategorie)value;
            return string.Join("", Enumerable.Repeat(" ", (k.Ebene -1)*4)) + k.Bezeichnung;
        }

       return "";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

How do I get rid of the spaces in the Text when the item is selected, but keep them in the Dropdownlist to show the hierarchy? Red square in the picture? Is there something like SelectedItemTemplate?

enter image description here

Mister 832
  • 1,177
  • 1
  • 15
  • 34
  • http://stackoverflow.com/questions/4672867/can-i-use-a-different-template-for-the-selected-item-in-a-wpf-combobox-than-for OR http://stackoverflow.com/questions/3995853/how-to-display-a-different-value-for-dropdown-list-values-selected-item-in-a-wpf?rq=1 – Nikhil Vartak Apr 23 '17 at 08:52
  • thanks. I really don't know why I didn't find those. – Mister 832 Apr 23 '17 at 10:06

0 Answers0