Not sure why I cant get this to work. I have a combobox in WPF. Want to loop through all the controls and set active where criteria are met. I find the match, but cant seem to set the value. this example is modeled after the "selected value" approach... Set SelectedItem of WPF ComboBox
bool match = false;
foreach (ComboBoxItem cbi in cb_Divisinos.Items)
{
if (cbi.Content.ToString().Split('-')[0].Trim() == family.Division.ToString()) {
cb_Divisinos.SelectedValue = cbi.Content.ToString();
match = true;
}
}
If I could see your XAML, it'd be helpful, but if I had to guess I would say that most likely you aren't setting the SelectedValuePath on the ComboBox element in XAML.
<ComboBox Grid.Row="1" Grid.Column="0"
Name="combo" SelectedValuePath="Content">
In order for this process to work correctly though, the items should also be defined in the XAML and not through a bound items source. If you are binding to an Items Source, then you would need to use the SelectedItem approach instead.
If I could currently do this as a comment, I would, but alas I've created a newer profile and I cannot.
If your ComboBox is explicitly populated with ComboBoxItems like this:
<ComboBox x:Name="cb_Divisinos">
<ComboBoxItem>Division A - xyz</ComboBoxItem>
<ComboBoxItem>Division B - abc</ComboBoxItem>
</ComboBox>
...you can simply set the SelectedItem property to the ComboBoxItem that you want to select:
bool match = false;
foreach (ComboBoxItem cbi in cb_Divisinos.Items)
{
if (cbi.Content.ToString().Split('-')[0].Trim() == family.Division.ToString())
{
cb_Divisinos.SelectedItem = cbi;
match = true;
break;
}
}
Another approach, which works better for me, is to select the item using SelectedIndex as following:
bool match = false;
int selectedIndexNumber = 0;
foreach (ComboBoxItem cbi in cb_Divisinos.Items)
{
if (cbi.Content.ToString().Split('-')[0].Trim() == family.Division.ToString()) {
cb_Divisinos.SelectedValue = cbi.Content.ToString();
match = true;
break;
}
selectedIndexNumber += 1;
}
then apply the selectedindex like...
cb_Divisinos.SelectedIndex = selectedIndexNumber;
in the ComboBox set the Binding for SelectedIndex...
<ComboBox Name="cb_Divisinos" ItemsSource="{Binding }"
DisplayMemberPath="Name"
SelectedValuePath="CategoryID" SelectedIndex="{Binding Mode=OneWay}">
</ComboBox>
You don't need to specify a value or fieldname for SelectedIndex Binding; just set it as i have shown above.
Related
Need help on below errors highlighted in the code comments shown below
The purpose of the below method is to find an combox item byvalue and set it to combobox if present else add it to combobox and then set it.
private void SetComboBoxValueHelper(ComboBox cb, string valuetoSet)
{
bool isValueNotFound = false;
cb.SelectedValue = valuetoSet;
isValueNotFound = string.IsNullOrEmpty(Convert.ToString(cb.SelectedValue));
if (isValueNotFound)
{
//try to ignore case and find the item in combobox
foreach (ComboBoxItem item in cb.Items) //1.ERROR AFTER ANY ITEM ADDED using my code
{
if (string.Compare(Convert.ToString(item.Content), valuetoSet, true) == 0)
{
cb.SelectedValue = item.Content;
isValueNotFound = false;
}
}
//if still not found add the item to the combobox
if (isValueNotFound)
{
cb.Items.Add(valuetoSet);
cb.SelectedValue = valuetoSet;//2.THIS IS NOT WORKING
}
}
}
A sample combobox i use is
<ComboBox Grid.Column="5" Grid.Row="4" Margin="10" Name="cbbox1" SelectedValuePath="Content">
<ComboBoxItem Content="No" IsSelected="True" />
<ComboBoxItem Content="Yes" />
</ComboBox>
Please let me know
a)how i can fix that non working line.
b)I get error at line shown in comment. how do i prevent it.
The problem here is that you are adding a string to the ComboBox's Items:
cb.Items.Add(valuetoSet);
You should instead add a new ComboBoxItem:
cb.Items.Add(new ComboBoxItem { Content = valuetoSet });
Otherwise you mix ComboBoxItems and strings in the Items collection. Now when you iterate over the items as ComboBoxItems, you get an exception when the added string item is encountered.
You should however consider to use string items instead of ComboBoxItems. That would make your code cleaner and you could address the selected string item directly by the SelectedItem property, without any need for SelectedValuePath and stuff like Convert.ToString(item.Content).
You could even define the initial item strings in XAML like this:
<ComboBox xmlns:sys="clr-namespace:System;assembly=mscorlib"
SelectedIndex="0" ...>
<sys:String>No</sys:String>
<sys:String>Yes</sys:String>
</ComboBox>
Now your whole SetComboBoxValueHelper method would simplify as Novitchi has written:
private void SetComboBoxValueHelper(ComboBox cb, string valuetoSet)
{
if (!cb.Items.Contains(valuetoSet))
{
cb.Items.Add(valuetoSet);
}
cb.SelectedItem = valuetoSet;
}
EDIT: If there is still any need to iterate over the items, you would also iterate over strings instead of ComboBoxItems:
foreach (string item in cb.Items)
{
...
}
As Clemens already suggested you shouldn't mix the ComboBoxItems and String. From XAML you adding to combobox ComboBoxItems, from code you adding Strings. A simple solution will be to set all Items as string. For this you should add your Yes, No items from code too.
Then your SetComboBoxValueHelper should look like this:
private void SetComboBoxValueHelper(ComboBox cb, string valuetoSet)
{
bool valueNotFound = !cb.Items.Contains(valuetoSet);
if (valueNotFound)
cb.Items.Add(valuetoSet);
cb.SelectedItem = valuetoSet;
}
The wpf will create the ComboBoxItem for you and you can get it using
cb.ItemContainerGenerator.ContainerFromItem("ItemString");
Quite a few posts around this area, but none are helping me... here's the scenario: I've got two "season" drop downs to simulate a range. If you pick a season in the begin range one, the viewmodele automatically sets the property bound to the end range to the same season (so it defaults to a single year and not a range. Here's what the XAML looks like (removed lot of the formatting attibutes for readability):
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}"
SelectedItem="{Binding SelectedBeginRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}"
SelectedItem="{Binding SelectedEndRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />
The properties in the view model look like this:
private Season _selectedBeginRangeSeason;
private const string SelectedBeginRangeSeasonPropertyName = "SelectedBeginRangeSeason";
public Season SelectedBeginRangeSeason {
get { return _selectedBeginRangeSeason; }
set {
if (_selectedBeginRangeSeason != value) {
var oldValue = _selectedBeginRangeSeason;
_selectedBeginRangeSeason = value;
RaisePropertyChanged<Season>(SelectedBeginRangeSeasonPropertyName, oldValue, value, true);
}
}
}
private Season _selectedEndRangeSeason;
private const string SelectedEndRangeSeasonPropertyName = "SelectedEndRangeSeason";
public Season SelectedEndRangeSeason {
get { return _selectedEndRangeSeason; }
set {
if (_selectedEndRangeSeason != value) {
Debug.WriteLine("Updating property SelectedEndRangeSeason...");
var oldValue = _selectedEndRangeSeason;
_selectedEndRangeSeason = value;
Debug.WriteLine("Broadcasting PropertyChanged event for property SelectedEndRangeSeason...");
RaisePropertyChanged<Season>(SelectedEndRangeSeasonPropertyName, oldValue, value, true);
}
}
}
private void UpdateSelectedSeasonSelectors() {
// if the end range isn't selected...
if (_selectedEndRangeSeason == null) {
// automatically select the begin for the end range
SelectedEndRangeSeason = _selectedBeginRangeSeason;
}
}
I've verified the end property is being changed both with the debug statements and with unit tests, but the UI isn't changing when I select it... can't figure out what's going on and have looked at this so many different ways...
Did you get the SelectedSeason from the AvailableSeasons collection? If not, did you implement anything special to compare Seasons?
For example, suppose you have
<ComboBox ItemsSource="{Binding AvailableSeasons}"
SelectedItem="{Binding SelectedSeason}" />
If SelectedSeason = new Season(); the SelectedItem binding won't work because new Season(); does not exist in AvailableSeasons.
You'll need to set SelectedSeason = AvailableSeasons[x] for SelectedItem to work because that makes the two items exactly the same. Or you can implement some custom method to compare the two seasons to see if they're the same. Usually I just overwrite the ToString() method of the class being compared.
Try to fire an event from the ViewModel to notify the UI to refresh the calendar.
<ComboBox Grid.Row="1" Grid.Column="0" Width="Auto" Name="cmbBudgetYear">
<ComboBoxItem Content="2009" />
<ComboBoxItem Content="2010" />
<ComboBoxItem Content="2011" />
<ComboBoxItem Content="2012" />
</ComboBox>
How do I set the selected item to the current year in the code behind?
Something like...
cmbBudgetYear.SelectedItem = cmbBudgetYear.Items(
get the item with the Now.Year.ToString)
There exist many ways to do this but for your example, I would change the ComboBox-Tag as follows:
<ComboBox Grid.Row="1" Grid.Column="0"
Name="cmbBudgetYear" SelectedValuePath="Content">
I added the attribute-defition SelectedValuePath="Content". After that you can set the value with a corresponding string, e.g.:
cmbBudgetYear.SelectedValue = "2009";
Take care that the value must be a string. For your example, use
cmbBudgetYear.SelectedValue = DateTime.Now.Year.ToString();
An additional idea
If you use the code-behind anyway, would it be a possibility to fill the combobox with integers. Someting like:
for(int y=DateTime.Now.Year;y>DateTime.Now.Year-10;y--){
cmbBudgetYear.Items.Add(y);
}
..then you can select the values extremly simple like
cmbBudgetYear.SelectedValue = 2009;
... and you would have also other advantages.
In my case I added the values manually with:
myComboBox.Items.Add("MyItem");
and then I select the wanted one with:
myComboBox.SelectedItem = "WantedItem";
instead of:
myComboBox.SelectedValue = "WantedItem";
In this case, you should be able to simply use .Text() to set it:
cmbBudgetYear.Text = "2010";
For getting the value after a change, though, and maybe it's because I didn't set SelectedValuePath="Content" everywhere, or maybe because I didn't use SelectedValue to set it (and why I'm mentioning it), it becomes slightly more complicated to determine the actual value, as you have to do this after adding the event handler for SelectionChanged in the XAML:
private void cmbBudgetYear_SelectionChanged(object sender, EventArgs e)
{
ComboBox cbx = (ComboBox)sender;
string yourValue = String.Empty;
if (cbx.SelectedValue == null)
yourValue = cbx.SelectionBoxItem.ToString();
else
yourValue = cboParser(cbx.SelectedValue.ToString());
}
Where a parser is needed because .SelectedValue.ToString() will give you something like System.Windows.Controls.Control: 2010, so you have to parse it out to get the value:
private static string cboParser(string controlString)
{
if (controlString.Contains(':'))
{
controlString = controlString.Split(':')[1].TrimStart(' ');
}
return controlString;
}
At least, this is what I ran into.... I know this question was about setting the box, but can't address only setting without talking about how to get it, later, too, as how you set it will determine how you get it if it is changed.
It works fine for me.
ObservableCollection<OrganizationView> Organizations { get; set; }
Organizations = GetOrganizations();
await Dispatcher.BeginInvoke((Action)(() =>
{
var allOrganizationItem = new OrganizationView() { ID = 0, IsEnabled = true, Name = "(All)" }; // It is a class
Organizations.Add(allOrganizationItem);
cbOrganizations.DisplayMemberPath = "Name";
cbOrganizations.SelectedValuePath = "ID";
cbOrganizations.ItemsSource = null;
cbOrganizations.ItemsSource = Organizations; // Set data source which has all items
cbOrganizations.SelectedItem = allOrganizationItem; // It will make it as a selected item
}));
I have the following XAML markup:
<TextBox x:Name="MyTextBox" Text="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Products}" DisplayMemberPath="ProductName"
SelectedValue="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber}"
SelectedValuePath="ProductNumber" />
My View's DataContext is bound to a viewmodel containing a public property called SelectedCustomer. Customer objects contain a FavouriteProduct property of type Product and Product objects contain public properties ProductNumber and ProductName.
The behaviour I'm looking for is to have the SelectedItem of the ComboBox update the Text in the TextBox and vice versa. ComboBox to TextBox works just fine. Selecting any product in the ComboBox updates the TextBox with the product number of that product. However when I try to go the other way I get som strange behaviour. It only works for the items that come before the selected item. I will try to explain:
Consider the following list of products ([Product Number], [Product Name]):
Fanta
Pepsi
Coca Cola
Sprite
Water
Now lets say that the SelectedCustomer's favourite product is Coca Cola (must be a developer). So when the window opens the TextBox reads 3 and the ComboBox reads Coca Cola. Lovely. Now lets change the product number in the TextBox to 2. The ComboBox updates it's value to Pepsi. Now try to change the product number in the TextBox to anything higher then the number for Coca Cola (3). Not so lovely. Selecting either 4 (Sprite) or 5 (Water) makes the ComboBox revert back to Coca Cola. So the behaviour seems to be that anything below the item that you open the window width from the list in the ItemSource does not work. Set it to 1 (Fanta) and none of the others work. Set it to 5 (Water) and they all work. Could this have to do with some initialisation for the ComboBox? Potential bug? Curious if anyone else have seen this behaviour.
UPDATE:
After reading Mike Brown's response I have created properties for SelectedProduct and SelectedProductNumber. The problem I am having with this is that as soon as you select something from the ComboBox you end up in an endless loop where the properties keep updatign each other. Have I implemented the OnPropertyChanged handler incorrectly or is there something I am missing? Here is a snippet of code from my ViewModel:
private int _SelectedProductNumber = -1;
public int SelectedProductNumber
{
get
{
if (_SelectedProductNumber == -1 && SelectedCustomer.Product != null)
_SelectedProductNumber = SelectedCustomer.Product.ProductNumber;
return _SelectedProductNumber;
}
set
{
_SelectedProductNumber = value;
OnPropertyChanged("SelectedProductNumber");
_SelectedProduct = ProductList.FirstOrDefault(s => s.ProductNumber == value);
}
}
private Product _SelectedProduct;
public Product SelectedProduct
{
get
{
if (_SelectedProduct == null)
_SelectedProduct = SelectedCustomer.Product;
return _SelectedProduct;
}
set
{
_SelectedProduct = value;
OnPropertyChanged("SelectedProduct");
_SelectedProductNumber = value.ProductNumber;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
UPDATE 2
I have changed the implementation slightly now by updating the SelectedCustomer.FavouriteProduct from both properties and then using that when reading their values. This now works but I'm not sure it's the 'correct way'.
private int _SelectedProductNumber = 0;
public int SelectedProductNumber
{
get
{
if (SelectedCustomer.Product != null)
_SelectedProductNumber = SelectedCustomer.Product.ProductNumber;
return _SelectedProductNumber;
}
set
{
_SelectedProductNumber = value;
SelectedCustomer.FavouriteProduct = ProductList.FirstOrDefault(s => s.ProductNumber == value);
OnPropertyChanged("SelectedProductNumber");
OnPropertyChanged("SelectedProduct");
}
}
private Product _SelectedProduct;
public Product SelectedProduct
{
get
{
if (SelectedCustomer.Product != null)
_SelectedProduct = SelectedCustomer.Product;
return _SelectedProduct;
}
set
{
_SelectedProduct = value;
SelectedCustomer.FavouriteProduct = value;
OnPropertyChanged("SelectedProduct");
OnPropertyChanged("SelectedProductNumber");
}
}
Your aim is not too clear so I have written the folloiwng so support either options I can see.
To keep two elements bound to one item in sync you can set the IsSynchronizedWithCurrentItem="True" on your combobox as shown below:
<TextBox x:Name="MyTextBox" Text="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding Products}" DisplayMemberPath="ProductName"
SelectedValue="{Binding Path=SelectedCustomer.FavouriteProduct.ProductNumber}"
IsSynchronizedWithCurrentItem="True"
SelectedValuePath="ProductNumber" />
This will mean everything in the current window bound to the same background object will keep in sync and not give the odd behaviours you are seeing.
This quote form this longer MSDN article describes the effect:
The IsSynchronizedWithCurrentItem
attribute is important in that, when
the selection changes, that is what
changes the "current item" as far as
the window is concerned. This tells
the WPF engine that this object is
going to be used to change the current
item. Without this attribute, the
current item in the DataContext won't
change, and therefore your text boxes
will assume that it is still on the
first item in the list.
Then setting the Mode=TwoWay as suggested by the other answer will only ensure that both when you update the textbox the underlying object will be updated and when you update the object the textbox is updated.
This makes the textbox edit the selected items text and not select the item in the combolist with the matching text (which is the alternative think you are may be trying to achieve?)
To achieve the synchronised selection effect it may be worth setting IsEditable="True" on the combobox to allow users to type items in and dropping the text box. Alternatively if you need two boxes replace the textbox with a second combobox with IsSynchronizedWithCurrentItem="True" and IsEditable="True" then a styled to make it like a text box.
What you want to do is expose separate properties on your ViewModel for the currently selected product and currently selected product number. When the selected product is changed, update the product number and vice versa. So your viewmodel should look something like this
public class MyViewModel:INotifyPropertyChanged
{
private Product _SelectedProduct;
public Product SelectedProduct
{
get { return _SelectedProduct; }
set
{
_SelectedProduct = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProduct"));
_SelectedProductID = _SelectedProduct.ID;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProductID"));
}
}
private int _SelectedProductID;
public int SelectedProductID
{
get { return _SelectedProductID; }
set
{
_SelectedProductID = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedProductID"));
_SelectedProduct = _AvailableProducts.FirstOrDefault(p => p.ID == value);
PropertyChanged(this,new PropertyChangedEventArgs("SelectedProduct"));
}
}
private IEnumerable<Product> _AvailableProducts = GetAvailableProducts();
private static IEnumerable<Product> GetAvailableProducts()
{
return new List<Product>
{
new Product{ID=1, ProductName = "Coke"},
new Product{ID = 2, ProductName="Sprite"},
new Product{ID = 3, ProductName = "Vault"},
new Product{ID=4, ProductName = "Barq's"}
};
}
public IEnumerable<Product> AvailableProducts
{
get { return _AvailableProducts; }
}
private Customer _SelectedCustomer;
public Customer SelectedCustomer
{
get { return _SelectedCustomer; }
set
{
_SelectedCustomer = value;
PropertyChanged(this, new PropertyChangedEventArgs("SelectedCustomer"));
SelectedProduct = value.FavoriteProduct;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
So now your XAML binds to the appropriate properties and the viewModel is responsible for syncrhronization
<TextBox
x:Name="MyTextBox"
Text="{Binding Path=SelectedProductID, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox
x:Name="MyComboBox"
ItemsSource="{Binding AvailableProducts}"
DisplayMemberPath="ProductName"
SelectedItem="{Binding SelectedProduct}" />
Don't forget to implement the rest of INotifyPropertyChanged and the GetAvailableProducts function. Also there may be some errors. I hand typed this here instead of using VS but you should get the general idea.
Try:
SelectedItem="{Binding Path=YourPath, Mode=TwoWay"}
instead of setting SelectedValue and SelectedValuePath.
Might work with SelectedValue too, don't forget the Mode=TwoWay, since this isn't the default.
A good approuch would to use the master detail pattern - bind the master (the items view, e.g. combobox) to the data source collection and the detail view (e.g. text box) to the selected item in the source collection, using a binding converter to read/write the appropriate property.
Here is an example:
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2008/03/28/63397.aspx
Notice the master binding is of the form {Binding} or {Binding SourceCollection} and the details binding is of the form {Binding } or {Binding SourceCollection}.
To get this working you need to wrap you collection with an object that keeps the selected item. WPF has one of these built-in: ObjectDataProvider.
Example:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/068977c9-95a8-4b4a-9d38-b0cc36d06446
I have been confused while setting SelectedItem programmaticaly in wpf applications with Net Framework 3.5 sp1 installed. I have carefully read about hundred posts \topics but still confused((
My xaml:
<ComboBox name="cbTheme">
<ComboBoxItem>Sunrise theme</ComboBoxItem>
<ComboBoxItem>Sunset theme</ComboBoxItem>
</ComboBox>
If I add IsSelected="True" property in one of the items - it's dosn't sets this item selected. WHY ?
And i was try different in code and still can't set selected item:
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 SelectedItem is not readonly property, so why it wan't work?
I think thats should be a Microsoft's problems, not my. Or I have missed something??? I have try playing with ListBox, and all work fine with same code, I can set selections, get selections and so on.... So what can I do with ComboBox ? Maybe some tricks ???
To select any item in the ComboBox and to set it as default item selected just use the below line:
combobox.SelectedIndex = 0; //index should be the index of item which you want to be selected
If i add the combobox and items programmatically, this works for me:
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);
It also works if it set the ItemSource property programmatically, then set the text to the selected value.
Create a public property in your viewmodel for the theme list and one for the selected item:
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");
}
}
bind your combobox in xaml to the properties like this:
<ComboBox
Name="cbTheme"
ItemsSource="{Binding ThemeList}"
SelectedItem="{Binding SelectedItem}">
</ComboBox>
now all you do is add items to the ThemeList to populate the combobox. To select an item in the list, set the selected property to the text of the item you want selected like this:
var tmpList = new List<string>();
tmpList.Add("Sunrise theme");
tmpList.Add("Sunset theme");
_viewModel.ThemeList = tmpList;
_viewModel.SelectedItem = "Sunset theme";
or try setting the selected item to the string value of the item you want selected in your own code if you want to use the code you currently have - not sure if it will work but you can try.
If you know the index of the item you want to set, in this case it looks like you are trying to set index 1, you simply do:
cbTheme.SelectedIndex = 1;
I found that when you don't know the index, that's when you have the real issue. I know this goes beyond the original question, but for Googlers on that count that want to know how to set the item when the index isn't known but the value you want to display IS known, if you are filling your dropdown with an ItemSource from a DataTable, for example, you can get that index by doing this:
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++;
}
}
}
And then you do simply do cbTheme.SelectedIndex = matchedIndex;.
A similar iteration of ComboBoxItem items instead of DataRow rows could yield a similar result, if the ComboBox was filled how the OP shows, instead.
Is the ComboBox data bound?
If so you are probably better to do it through Binding rather than code ....
See this question ... WPF ListView Programmatically Select Item
Maybe create a new SelectableObject {Text = "Abc Theme", IsCurrentlySelected = True}
Bind a collection of SelectableObjects to the ComboBox.
Essentially setting the IsCurrentlySelected property in the model and having UI update from the Model.
Acording Answer 4
If you already add the Items in the Item source.
Fire the PropertyChangedEvent of the selectet Value.
tmpList.Add("Sunrise theme");
tmpList.Add("Sunset theme");
PropertyChangedEvent.Notify(this,"SelectedItem");