Set the text and value of a ComboBoxItem - silverlight

I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like to set their text (the text that is visible for the end-user) and their value (the object that I will handle in the background after the user has selected it.
However the ComboBoxItem seems to only have one member for these two requirements: the Content variable. At the same time this would not fit my needs as I want to distinguish the text and value properties and want to do this without data binding. Is there some viable solution to achieve this?
My current code looks as follows:
ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);

Guess you can use the Tag property.

Related

WPF DataGrid checkbox binding

I'm not sure what I'm missing here, but I'm having trouble getting a checkbox to bind to a list properly. The rest of the properties of the list bind just fine, but the checkbox is having issues. Here's what I have:
In the class that serves as the template for each object in the list I have:
Property Process As New CheckBox
In the MainWindow_Loaded Event I have:
Dim ProcessCol As new DataGridCheckBoxColumn
ProcessCol.Header = "P?"
ProcessCol.IsReadOnly = False
...
InputGrid.ItemsSource = InputData 'Which is a list of my Order Allocation objects which contains the checkbox property
...
Dim ProcessBinding As New Binding("Process")
ProcessBinding.Mode = BindingMode.TwoWay
ProcessCol.Binding = ProcessBinding
...
InputGrid.Columns.Add(ProcessCol)
When I try to populate this collection and look at the items I get checkbox = nothing. I'm not sure what I'm missing here... I know I couldn't be too far off...
Edit: I changed the property to "new CheckBox" and now I get an initialzied checkbox object in the list item as "System.Windows.Controls.CheckBox Content: IsChecked:False which in this case should have been true. So maybe a step closer, but still not there.
I found an answer here: WPF: CheckBox in DataGrid
Technically not an answer to my original question, but it works. Rather than a checkbox property in my class I now have a boolean property. The column is still created as a checkbox column. It works.
If a column can be bound to a chebox property of a list I'd still be interested in hearing that, but for me this solution works.

Get item text from listbox

I'm making a UserControl which uses a ListBox. In some moment I need to get the text of the selected item in the ListBox. The "normal" way I would go is:
var selected = (CustomObject)listBox.SelectedItem;
var str = selected.PropertyShowingInListBox;
But for some reasons of the UserControl I can't cast the SelectedItem. So my next chance is to get the text by reflection using the SelectedItem as object and DisplayMemberPath. Like this:
var selected = listBox.SelectedItem;
var str = selected.GetType().GetProperty(listBox.DisplayMemberPath).GetValue(selected, null).ToString();
But it isn't the best. Is there any way I use?

Updating WPF TextBox properties after setting Text

When setting the Text property of a WPF TextBox control, other properties that should also change (as a side effect) do not change. In particular, I would like to check the value of the ExtentWidth property after setting Text, but it does not change. I've tried calling UpdateLayout() to no avail. In Windows.Forms, I would call DoEvents().
OK, here's some code. I put this in the Window_Loaded() event handler. The problem is that textBox.ExtentWidth doesn't change when textBox.Text changes. That doesn't really surprise me. I figure I need to call something like textBox.UpdateLayout() to make it recalculate ExtentWidth, but that didn't help. ExtentWidth does vary depending on what I initialize textBox.Text to in the Window's constructor, but that doesn't help me. I need to set several different Text values and get the corresponding ExtentWidth for each.
string initText = textBox.Text; // "textBox"
double extentWidth = textBox.ExtentWidth; // 39.3
textBox.Text = "short text";
extentWidth = textBox.ExtentWidth; // 39.3
textBox.Text = "Long enough to make a difference, eh?";
extentWidth = textBox.ExtentWidth; // 39.3
I found a solution to the specific problem of getting TextBox.ExtentWidth to change after setting Text. Setting Text will raise the LayoutUpdated event, and you can get the new value of ExtentWidth in a handler for LayoutUpdated.
I used this fact to create a subclass of WPF TextBox that displays an ellipsis when the text is too long for the visible area. I wrote a CodeProject article about it here.

properties in ComboBox in WPF

Why i cant set selecteditem property programatically?
Im calling it from another XAML Window, that hace certaing controls , one of them is a ComboBox i was trying this :
string tm = (from ea in db.EXAMENXATENCIONs where ea.codigo == Convert.ToInt32(numeroinforme) select ea.turnomedico).FirstOrDefault();
demo.cboTurnoMed.SelectedItem = tm;
demo.cboTurnoMed.Text = tm;
C# 3.5
Thanks!
Is the item you're trying to set as selected well in the ComboBox data source?
The SelectedItem property looks for the value you provide in data source and then select it if found.
I'm not certain what type of object your ComboBox has in it, but you might try setting the SelectedValue rather than the SelectedItem.
You can set the SelectedItem. But the objects must MATCH. They can't just have the same data, they must actually be the same object.
What you're doing when you set the SelectItem property is saying, "You (the combobox) have a collection of objects, and I want this particular one in your list to be the selected one". You are not actually giving the combobox a new item, if that clears it up.

How to bind a custom value to the Text property of a TextBlock inside of a ListBox DataTemplate?

I would like my ListBox to number each ListItem using its index + 1.
How would I do that to the Text property of a TextBlock in a DataTemplate of the ListBox?
If each ListBoxItem uses SelectedIndex + 1, they will all display the same value since SelectedIndex is a scalar. Moreover, this number will change as the user selects different ListBoxItems. I suspect you actually want to display each item's index within the ListBox + 1.
To achieve this, you're probably best off using the ListBox's ItemsContainerGenerator to get the index of the item within the container (see the IndexFromContainer method). You could look at exposing this from your data class, or perhaps look into an attached readonly property that retrieves this value for you.
I had the same question. So far I'm just using my data model to provide the numbers...

Resources