1

I have a ComboBox with static Items

<ComboBox SelectedItem="{Binding SelectedOperator}" >
     <ComboBoxItem Content="=" IsSelected="True" />
     <ComboBoxItem Content="&gt;" />
     <ComboBoxItem Content="&lt;" />                    
</ComboBox>

but the first item is not selected although IsSelected="True" is set.

Can I select it in WPF somehow? I only want to do it in code-behind if really necessary.

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Personally, I think I'd just create an ObservableCollection in your ViewModel or codebehind, fill it with your operators, and set the ItemsSource of the ComboBox to the collection. See: http://stackoverflow.com/a/19632600/1095741 for a similar approach. – goobering Aug 26 '15 at 10:29

4 Answers4

2

You might probably want to use the ComboBoxItem's Content string for the SelectedOperator binding anyway, in which case you could use a FallbackValue:

<ComboBox SelectedValuePath="Content"
          SelectedValue="{Binding SelectedOperator, FallbackValue=\=}">
    <ComboBoxItem Content="=" />
    <ComboBoxItem Content="&gt;" />
    <ComboBoxItem Content="&lt;" />
</ComboBox>
Clemens
  • 123,504
  • 12
  • 155
  • 268
1

If you absolutely have to keep everything in XAML, it may be viable to name the ComboBox and bind to its SelectedItem from other elements. You won't have a binding to SelectedOperator for use in your VM/codebehind, but this may not be a deal-breaker depending on how your application is set up.

<Grid x:Name="LayoutRoot" HorizontalAlignment="Center">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox x:Name="MyComboBox"
            Grid.Row="0"
            Width="80"
            Height="25">
        <ComboBoxItem Content="=" IsSelected="True" />
        <ComboBoxItem Content="&gt;" />
        <ComboBoxItem Content="&lt;" />
    </ComboBox>
    <TextBox Grid.Row="1" Text="{Binding ElementName=MyComboBox, Path=SelectedItem.Content}" />
</Grid>
goobering
  • 1,547
  • 2
  • 10
  • 24
0

How is your ComboBox gonna choose It's SelectedItem?

You set SelectedItem to "{Binding SelectedOperator}" and at the same time trying to set the IsSelected property of the first item to True.

Either set SelectedItemBinding

<ComboBox SelectedItem="{Binding SelectedOperator}" >
     <ComboBoxItem Content="=" />
     <ComboBoxItem Content="&gt;" />
     <ComboBoxItem Content="&lt;" />                    
</ComboBox>

Or set IsSelected property:

<ComboBox>
     <ComboBoxItem Content="=" IsSelected="True" />
     <ComboBoxItem Content="&gt;" />
     <ComboBoxItem Content="&lt;" />                    
</ComboBox>
Bahman_Aries
  • 4,658
  • 6
  • 36
  • 60
  • Both of these work fine - if you use the first approach you need to set SelectedOperator in your ViewModel/codebehind somewhere, and make sure that you've implemented INotifyPropertyChanged. If you use the second approach it should select the '=' option, but it won't set SelectedOperator as there's no binding. – goobering Aug 26 '15 at 10:50
  • @goobering: Did you try it? The second option does not select the item in the GUI. – juergen d Aug 26 '15 at 10:52
0

Hope this helps.

    <ComboBox Margin="53,119,35,157"
              SelectedValue="{Binding SelectedOperator}"
              SelectedValuePath="Content">
        <ComboBoxItem Content="=" />
        <ComboBoxItem Content="&gt;" />
        <ComboBoxItem Content="&lt;" />
    </ComboBox>

Code Behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var v = new ViewModel();
        this.DataContext = v;
    }
 }

ViewModel:

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        SelectedOperator = "=";
    }

    private string _selectedOperator;

    public string SelectedOperator
    {
        get { return _selectedOperator; }
        set { _selectedOperator = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}