0

I am using a Combobox whose ItemSource is ObservableCollection(i.e. ConversationList) of type .

<ComboBox x:Name="ConvId"
                  Grid.Row="2"
                  Width="75"
                  Height="23"
                  Margin="6,94,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  ItemsSource="{Binding ConversationList,
                                        UpdateSourceTrigger=PropertyChanged}"
                  SelectedItem="{Binding SelectedId,
                                         Mode=TwoWay}">

My requirement is : when there is no int value in Collection then Combobox Left side should display 'ConvIdenter image description here' which is a string. Fig. is shown below

I have workaround i.e to Convert collection from int to string and put 'ConvId' on 0th location and mark SelectedIndex= 0. but its not we want. Do I have to use some Custom control for this. Is there any to acheive this in XAML.

chriga
  • 798
  • 2
  • 12
  • 29
deathrace
  • 908
  • 4
  • 22
  • 48

3 Answers3

0

This Stack thread seems to do what you want cleanly with a Converter. How to display default text "--Select Team --" in combo box on pageload in WPF?

The answer I am referring to starts with this:

<Grid>
    <ComboBox
        x:Name="comboBox1"
        ItemsSource="{Binding MyItemSource}"  />
    <TextBlock
        Visibility="{Binding SelectedItem, ElementName=comboBox1, Converter={StaticResource NullToVisibilityConverter}}"
        IsHitTestVisible="False"
        Text="... Select Team ..." />
</Grid>
Community
  • 1
  • 1
Xcalibur37
  • 2,305
  • 1
  • 17
  • 20
0

Have below textblock below your combobox and make sure that both combobox and textblock are overlapping each other(i.e. they should be in same grid row)

<TextBlock Text="ConvID"
           IsHitTestVisible="False">
                <TextBlock.Style>
                    <Style
                            TargetType="TextBlock">
                        <Setter
                                Property="Visibility"
                                Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger
                                    Binding="{Binding ConversationList.Count}"
                                    Value="0">
                                <Setter
                                        Property="Visibility"
                                        Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
                </TextBlock>

Hope this helps.

user1246682
  • 206
  • 2
  • 7
0

Have a look at the WatermarkService by John Myczek. You can have it display a default string if no item is selected in the ComboBox. There's some issues with ComboBox in the answer I linked to, but if you look further down there's a fix for that.

Community
  • 1
  • 1
Eirik
  • 4,135
  • 27
  • 29