DotNetDeveloper
DotNetDeveloper

Reputation: 109

WPF Default Value (using Style Trigger) with Binding

I'm new to WPF so please bear with me. I have a ComboBox on my WPF window, the ItemSource property is bound to a List of strings property (Countries) and the SelectedItem is bound to a string property (SelectedCountry). Both of these properties are in the code behind - and I'm setting the DataContext to "this" (i.e. the Window).

The ComboBox xaml is:

<ComboBox Name="CountryComboBox" 
    VerticalAlignment="Center" 
    Width="200"
    ItemsSource="{Binding Path=Countries, Mode=OneTime}"
    SelectedItem="{Binding Path=SelectedCountry, Mode=TwoWay}">
</ComboBox>

I wanted to have a default "- Please Select -" option that is displayed when an item is not selected, therefore I placed the following xaml in App.xaml:

<Style TargetType="ComboBox">
    <Style.Triggers>
        <Trigger Property="SelectedItem" Value="{x:Null}">
            <Setter Property="IsEditable" Value="true" />
            <Setter Property="IsReadOnly" Value="true" />
            <Setter Property="Text" Value="- Please Select -" />
        </Trigger>
    </Style.Triggers>
</Style>

When my window is first displayed, the combobox does have the "- Please Select -" text as expected. When I then select a value in the combobox, the SelectedCountry gets populated appropriately, but then when I assign "null" to the SelectedCountry property the combobox still has the same selected country when I'd expect it to go back to "- Please Select -". What am I doing wrong?

Thanks.

Upvotes: 2

Views: 5826

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43626

It may be a better option not to modify the ComboBox and simply overlay a TextBlock over the ComboBox when the SelectedItem is null.

Just wrap the ComboBox and a TextBlock in a Grid and set a DataTrigger on the TextBlock to check if the SelectedItem is null and toggle its Visibility

Example:

    <Grid> 
        <ComboBox x:Name="combo" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedItem}" />
        <TextBlock x:Name="textblock" Text="- Please Select -" Margin="5,3,0,0" IsHitTestVisible="False">
            <TextBlock.Style>
             <Style TargetType="TextBlock">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedItem,ElementName=combo}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </Grid>

Result:

enter image description here

enter image description here

enter image description here

Upvotes: 4

Malcolm O&#39;Hare
Malcolm O&#39;Hare

Reputation: 5009

You need to insert a record into the Countries list that has a value of null and the name " - Please Select - ".

Alternatively I suppose you could extend the ComboBox control and write your own so that you could specify the null vale in the list without having to put a record into the Countries selection.

But of the two, it is much easier to just add a record to Countries.

Upvotes: 1

Related Questions