2

i want a default text on my combobox as i have binded my combo box to certain list of items...here is my code of xaml file.

<ComboBox x:Name="ProjectComboBox" 
          Text="{Binding ProjectNameBinding}" 
          ItemsSource="{Binding projectList, ElementName=MainWin}"
          SelectedValuePath="_id" DisplayMemberPath="_name"  
          SelectedItem="{Binding ProjectNameBindingClass, Mode=OneWayToSource}" 
          Width="130" Background="White" BorderThickness="1"  
          FontFamily="/TimeSheet;component/Resources/#Open Sans" FontSize="12" 
          Canvas.Right="159" Canvas.Top="8" Height="47">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding _name}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Safwan
  • 171
  • 1
  • 17
  • it helps, if you ask a concrete question on the end of the text. it also if you describe what you already tried to achieve that - so you show own efforts instead of blindly letting others work for you. ;-) – L-Ray Mar 21 '14 at 12:43
  • Possible duplicate http://stackoverflow.com/questions/1426050/how-to-display-default-text-select-team-in-combo-box-on-pageload-in-wpf – Sajeetharan Mar 21 '14 at 12:44

4 Answers4

2

Have you tried Text Property

<ComboBox x:Name="ProjectComboBox"
  IsEditable=True
  Text="{Binding ProjectNameBinding}" ....../>

or

You can use SelectedIndex Property and set it to 0(SelectedIndex=0) which displays first item in Source given.

or

you can do like this as in the link How to display default text "--Select Team --" in combo box on pageload in WPF?

Community
  • 1
  • 1
1

I achieved it atlast by taking a new textblock and applying a trigger on it,and using the IsNullConverter class it helped

<TextBlock Text="Select Project" IsHitTestVisible="False" FontFamily="/TimeSheet;component/Resources/#Open Sans" FontSize="14" Canvas.Right="191" Canvas.Top="22">
                        <TextBlock.Resources>
                            <Converters:IsNullConverter x:Key="isNullConverter"/>
                        </TextBlock.Resources>
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=ProjectComboBox,Path=SelectedItem,Converter={StaticResource isNullConverter}}" Value="False">
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
Safwan
  • 171
  • 1
  • 17
0

Try:

DefaultText="Not Specified"
Community
  • 1
  • 1
Chris
  • 915
  • 8
  • 28
  • I think there is no property like "DefaultText" in combobox and also in text block – Safwan Mar 21 '14 at 12:30
  • I cannot change the C# code as i have my xml file binded to it i.e to the list of combobox ,can we have any solution using XAML code only – Safwan Mar 21 '14 at 12:41
0

Try this in page load or form load

 comboBox.SelectedItem = null;
 comboBox.Text = "---select an item---";
user3417203
  • 25
  • 1
  • 2
  • 9