3

I'm trying to bind a ComboBox SelectedValue to a string. The Binding works flawlessly. However, one of my ComboBoxItem's IsSelected is set to True, but for some reason when I launch the application, none of the items is selected, the SelectedValue is blank and I need to re-select the item I want.

Here's my code:

XAML:

<ComboBox x:Name="SearchOptions" 
          FontFamily="Times New Roman" 
          Foreground="DarkRed"
          VerticalContentAlignment="Center" 
          HorizontalContentAlignment="Center"
          Grid.Column="2" Margin="10,0,0,0" Height="20"
          SelectedValue="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

    <ComboBoxItem x:Name="Contact" Content="A" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center" IsSelected="True"/>
    <ComboBoxItem x:Name="Paper" Content="B" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center"/>

</ComboBox>

ViewModel Code-Behind:

private string m_serachType;
public string SearchType
{
    get { return m_serachType; }
    set
    {
        m_serachType = value;
        OnPropertyChanged("SearchType");
    }
}

My ViewModel class implements INotifyPropertyChanged.

Any ideas?

Yael
  • 1,566
  • 3
  • 18
  • 25
Idanis
  • 1,918
  • 6
  • 38
  • 69

3 Answers3

2

Try using string insted of ComboboxItem:

MainWindow(XAML)

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid>
        <ComboBox SelectedItem="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   >            
            <sys:String>A</sys:String>
            <sys:String>B</sys:String>
        </ComboBox>
    </Grid>
</Window>

MainWindow (cs)

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new MyViewModel() { SearchType = "A" };
}

MyViewModel

class MyViewModel : INotifyPropertyChanged
{

    private string m_serachType;
    public string SearchType
    {
        get { return m_serachType; }
        set
        {
            m_serachType = value;
            OnPropertyChanged("SearchType");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
  • Tried both...Without success...The designer shows the string correctly, but it goes blank when I run the application. – Idanis Jun 08 '15 at 17:01
  • Do you set the `SearchType` when the application start running? – Hossein Narimani Rad Jun 08 '15 at 17:33
  • My code was very close to yours. The combination of `SelectedItem` and the initialization in the constructor is what eventually solved it. Thanks a lot for your patience and help!!! – Idanis Jun 08 '15 at 20:41
0

You can use SelectedIndex to force the control to pre-select a value.

<ComboBox SelectedIndex="1" ... />
Mike Eason
  • 9,525
  • 2
  • 38
  • 63
0

If you remove SelectedValue attribute your code will work. And then where you need you can do something like this:

var item = (ComboBoxItem)SearchOptions.SelectedItem;
string text = item.Content;
ivamax9
  • 2,601
  • 24
  • 33