Programmatically install ComboBox SelectedItem in WPF (3.5sp1)

I got confused when installing SelectedItem programmaticaly in wpf applications with installed installation of Net Framework 3.5 sp1. I carefully read about a hundred posts \ topics, but still confused ((My xaml:

 <ComboBox name="cbTheme">
    <ComboBoxItem>Sunrise theme</ComboBoxItem>
    <ComboBoxItem>Sunset theme</ComboBoxItem> 
 </ComboBox>

If I add the IsSelected = "True" property to one of the elements, it does not set this element. WHAT FOR? And I tried to distinguish the code and cannot set the highlighted element:

cbTheme.SelectedItem=cbTheme.Items.GetItemAt(1); //dosn't work
cbTheme.Text = "Sunrise theme"; //dosn't work
cbTheme.Text = cbTheme.Items.GetItemAt(1).ToString();//dosn't work
cbTheme.SelectedValue = ...//dosn't work
cbTheme.SelectedValuePath = .. //dosn't work
//and even this dosn't work:
ComboBoxItem selcbi = (ComboBoxItem)cbTheme.Items.GetItemAt(1);//or selcbi = new ComboBoxItem
cbTheme.SelectedItem = selcbi;

The selected item is not readonly, so why doesn't it work? I think these are Microsoft problems, not mine. Or am I missing something ??? I tried playing with ListBox, and everything works fine with the same code, I can set the selection, get the selection, etc. So what should I do with the ComboBox? Maybe some tricks ???

+5
6

ComboBox , :

combobox.SelectedIndex = 0; //index should be the index of item which you want to be selected
+7

, :

ComboBox newCombo = new ComboBox();

ComboBoxItem newitem = new ComboBoxItem();
newitem.Content = "test 1";
newCombo.Items.Add(newitem);
newitem = new ComboBoxItem();
newitem.Content = "test 2";
newCombo.Items.Add(newitem);
newitem = new ComboBoxItem();
newitem.Content = "test 3";
newCombo.Items.Add(newitem);

newCombo.SelectedItem =  ((ComboBoxItem)newCombo.Items[1]);
newCombo.Text = ((ComboBoxItem)newCombo.Items[1]).Content.ToString();

newStack.Children.Add(newCombo);

, ItemSource, .

+4

:

    private IEnumerable<string> _themeList;
    public IEnumerable<string> ThemeList
    {
        get { return _themeList; }
        set
        {
            _themeList = value;
            PropertyChangedEvent.Notify(this, "ThemeList");
        }
    }
    private string _selectedItem;
    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            PropertyChangedEvent.Notify(this,"SelectedItem");
        }            
    }

combobox xaml :

    <ComboBox 
        Name="cbTheme" 
        ItemsSource="{Binding ThemeList}"      
        SelectedItem="{Binding SelectedItem}">
    </ComboBox>

, , ThemeList combobox. , , :

    var tmpList = new List<string>();
    tmpList.Add("Sunrise theme");
    tmpList.Add("Sunset theme");

    _viewModel.ThemeList = tmpList;
    _viewModel.SelectedItem = "Sunset theme";

, , , , - , , .

+4

ComboBox?

, , , , ....

... WPF ListView Programmatically Select Item

, SelectableObject {Text = "Abc Theme", IsCurrentlySelected = True} SelectableObjects ComboBox.

, IsCurrentlySelected UI .

0

4

. PropertyChangedEvent .

tmpList.Add("Sunrise theme"); 
    tmpList.Add("Sunset theme");
    PropertyChangedEvent.Notify(this,"SelectedItem");
0

, , , 1, :

cbTheme.SelectedIndex = 1;

, , , . , , Googlers , , , , , , , ItemSource a DataTable, , , :

int matchedIndex = 0;
if (dt != null & dt.Rows != null)
{
    if (dt.Rows.Count > 0)
    {
        foreach (DataRow dr in dt.Rows)
        {
            string myRowValue = dr["SOME_COLUMN"] != null ? dr["SOME_COLUMN"].ToString() : String.Empty;
            if (myRowValue == "Value I Want To Set")
                break;
            else
                matchedIndex++;
        }
     }
 }

cbTheme.SelectedIndex = matchedIndex;.

ComboBoxItem DataRow , ComboBox , OP.

0

All Articles