Binding ComboBox SelectedValue to string disables default SelectedValue wpf

I am trying to bind ComboBox SelectedValue

to string

. Binding

works flawlessly. However, one of mine is ComboBoxItem IsSelected

set to True

, but for some reason when the app starts, none of the items are selected, SelectedValue

empty and I need to reselect 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:

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

      

My class ViewModel

implements INotifyPropertyChanged

.

Any ideas?

+3


source to share


3 answers


Try using string

insted 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));
        }
    }
}

      

+2


source


You can use SelectedIndex

to force the control to preselect a value.



<ComboBox SelectedIndex="1" ... />

      

0


source


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

var item = (ComboBoxItem)SearchOptions.SelectedItem;
string text = item.Content;

      

0


source







All Articles