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");
Related
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.
I have
a collection of StackPanel which each one includes a dynamic set of controls (based on database values), I want to set them as ItemsSource of some ComboBox
for example i have two database values which should be generated:
In DB i have these:
row 1=>Class [a] p [B] , [AB]vb
row 2=>Class tpy rbs=[sdfg],kssc[h] hm
and each one should generate as a ComboBox column like the fallowing:
In ComboBox I wanna generate these :
ComboBoxItem 1 :Class [a textBox] p [a textBox] , [a textBox]vb
ComboBoxItem 2 :Class tpy rbs=[a textBox].kssc[a textBox] hm
the fallowing code is doing this right:
Class ConvertToControlsFormat()
{
Regex exp = new Regex(#"\[\w*\]");
var source = new TestEntities().cmbSources;
foreach (var item in source)
{
StackPanel p = new StackPanel { Orientation = Orientation.Horizontal, FlowDirection = FlowDirection.LeftToRight };
int i = 0;
foreach (string txt in exp.Split(item.Title))
{
p.Children.Add(new TextBlock { Text = txt });
if (i < exp.Matches(item.Title).Count)
p.Children.Add(new TextBox { Text = exp.Matches(item.Title)[i].Value, Width = 30 });
}
cmb.Items.Add(p);
}
}
But I cant set TwoWay DataBindings for that, so I created a list of StackPanel as a field of cmbSource class (which is bound to ItemsSource of the ComboBox)
public partial class cmbSource
{
#region Primitive Properties
int iD;
public virtual int ID
{
get
{
if (Title != null)
ControlsCollection = SetControlsCollection(Title);
return iD;
}
set
{
iD = value;
}
}
private StackPanel SetControlsCollection(string ttl)
{
Regex exp = new Regex(#"\[\w*\]");
StackPanel p = new StackPanel { Orientation = Orientation.Horizontal, FlowDirection = System.Windows.FlowDirection.LeftToRight };
int i = 0;
foreach (string txt in exp.Split(ttl))
{
p.Children.Add(new TextBlock { Text = txt });
if (i < exp.Matches(ttl).Count)
p.Children.Add(new TextBox { Text = exp.Matches(ttl)[i].Value, Width = 30 });
}
return p;
}
public virtual string Title
{
get;
set;
}
public virtual StackPanel ControlsCollection
{
get;
set;
}
#endregion
}
but I have no idea of how bind it to ItemsSource of my ComboBox
Summery:I want to bind a list of controls to a ComboBox
any suggestions!? thank you.
EDIT
First: you do not bind a ComboBox to a collection of UI Elements. That is not the way WPF works. Container controls such as the Grid, StackPanel and Canvas can contain child controls. ItemsControls such as the ComboBox contain data objects and use DataTemplates to display the items.
Secondly: if the database can contain ANY data that could cause ANY UI to be needed you will need to generate the UI in code by creating StackPanels etc. adding controls and bindings as you do in your code examples.
Thirdly: the reason you can't bind is that the data from the database is a string that you split into parts; there is no way you can simply go back to the string.
Suggestion: the string in the database is probably (I hope) in some sort of format. Using that knowledge you could generate a new format string when you are parsing the database string. E.g., when the database contains foo [bar] you could generate {0} [bar]. On a save action from the user you could use that string to create the updated string for the database by using: String.Format("{0} [bar]", someControl.Text)
Extra: Please, next time, use better names and example texts; the question is unreadable like this. There is no way you can expect us to understand 2=>Class tpy rbs=[sdfg],kssc[h] hm
OLD ANSWER
Make a class Stuff, implementing INotifyPropertyChanged and having the properties Name and Value.
Load the database data into an ObservableCollection<Stuff> and bind the ComboBox to this collection.
Set the ItemTemplate of the combo box to a datatemplate like this:
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBox Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
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
}));
In a datagrid I have two DataGridComboBoxColumns. The items of one of these columns should depend on what is selected in the other column. The underlying collection used to model this is a dictionary<string,List<string>>. How should i go about implementing this? I can't seem to hook up to any relevant events on the columns, and I cant find any databinding scenarios that support this..
I had the same scenario a while back and fixed it like this:
public class DataItem : INotifyPropertyChanged {
...
public List<SomeObject> DisplayableComboBoxItems {
get; set;
}
private static Dictionary<int, List<SomeObject>> myDict;
public Dictionary<int, List<SomeObject>> MyDict {
get {
if (myDict == null) {
myDict = GetYourDataFromSomewhere();
}
return myDict;
}
}
public int TypeId {
get { return typeId; }
set {
if (value == typeId) return;
typeId = value;
RaisePropertyChanged("TypeId");
}
}
public int TypeSetId {
get { return typeSetId; }
set {
if (typeSetId == value) return;
typeSetId = value;
RaisePropertyChanged("TypeSetId");
DisplayableComboBoxItems = MyDict[typeSetId];
RaisePropertyChanged("DisplayableComboBoxItems");
TypeId = 0;
}
}
...
}
DataItem is the object that gets bound to a DataRow.
This is just a small mock-up of the code. Basically, whenever the TypeSet changes, I needed a new list of Types to be displayed. I used just a static list, in this example i used a dictionary.
With this setup you can bind you combobox ItemsSource to the 'DisplayableComboBoxItems', and your SelectedValue to "TypeId".
You're gonna need other properties to display the correct text instead of the TypeId.
The downside of this is that when you have 1000+ items, you'll have that same list for all items. This wasn't however the case with me (DataGrid showed max 50 items).
I hope this is clear enough and that it helps you in the right direction!
cheers!
Roel
Instead of using a DataGridComboBoxColumn for the second column, I went with a DataGridTemplateColumn with an embedded Combobox. For the itemsource i defined a converter: string -> List<string>. The converter translates the value of the selecteditem of the other DataGridComboBox (which is bound to Navn) into List<string>, this is just a dictionary lookup.
Like so:
<my:DataGridTemplateColumn>
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Værdi}"
ItemsSource="{Binding Navn, Converter={StaticResource dimensionToValues}}"
>
</ComboBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>