How to display a different value in a WPF combobox based on selection

I couldn’t figure out a great way to title this post. Anyway, the issue I had was having a (none) value in the combobox but when it is selected show a blank combo. Those were the requirements and I wanted to do an easy solution without having to write a converter. Here is the solution using a simple style. Note, the StringResources line is where the source of the (none)string lives, You could just hard code this to your string if you aren’t pulling from a resource file.


    <!-- Style for a combobox so that if the none value is picked, it will show up as a blank string.-->
    <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
        <Setter Property="Foreground" Value="DarkBlue"/>
        <Setter Property="Margin" Value="0,2,0,2"/>
        <Setter Property="ItemTemplate">
                    <TextBlock Margin="0">
                            <Style TargetType="TextBlock">
                                    <Setter Property="Text" Value="{Binding}"/>
                                    <MultiDataTrigger><!-- handle the display struct-->
                                            <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem},FallbackValue={x:Null}}" Value="{x:Null}"/>
                                            <Condition Binding="{Binding Text}" Value="{x:Static StringResources:WorkProgram.NoneValue}"/>
                                            <Setter Property="Text" Value=" "/>

Hapy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *