properties in ComboBox in WPF - 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.

Related

WPF TreeView ItemsSource not keeping values

I am using a wpf treeview and binding the ItemsSource to an IEnumerable of my ViewModel that has an IsChecked Property. I am binding a checkbox to this value with a Mode of TwoWay. I can see when I step through the program that it is setting this value properly on my ViewModel when I check the checkbox.
I then have a Menu Item that "Runs Checked". In this method I have a foreach loop that runs through the ItemsSource as IEnumerable of ViewModel looking for IsChecked = true to queue up the checked items to be run by a separate program. As such:
foreach (AccountViewModel account in tvClientList.ItemsSource as IEnumerable<AccountViewModel>)
{
if (account.IsChecked)
{
context.Queues.InsertOnSubmit(new Queue {Id = account.Id});
}
}
However, account.IsChecked is always false. Why is this?
i would check the following.
does the setter of AccountViewModel.Ischecked is called when clicking the treeview checkbox. if no - then thats the problem if yes you should look at your collection bindings.
debug your foreach and check wether the treeviewitemssource is the collection you expect.
maybe you can post your bindings, if that all no help.
ps: dont access your View controls directly if you use viewmodels/mvvm.
Just for anyone else encountering this issue. ItemsSource returns a new set of data, Items, however contains the current set of data.

Silverlight: How can I set a converter on a ComboBox ItemsSource in the code-behind?

I have a combobox that is populated at runtime with values from a loadoperation (I'm using RIA services)
cboSite.ItemsSource = lo.Entities;
However, I want to be able to add a null item to the top of the list shown in the combobox, so following the example given here:
http://clr-namespace.com/post/SilverlightWPF-ComboBox-with-Empty-Item-allows-user-to-go-back-to-no-selection.aspx
I am trying to use a converter to insert the item at the top of the list. However, the problem I have is that I can't seem to work out how to specify the converter in the code behind!
Any ideas how to achieve this?
If you are willing to assign ItemsSource from the code-behind you can convert your Entities in the same very place. Something like this:
var converter = new AddEmptyItemConverter();
var converted = (IEnumerable<Entity>)converter.Convert(lo.Entities,
typeof(IEnumerable<Entity>),
null,
null);
cboSite.ItemsSource = converted;
That Entity should be the type of Entities collection element.

Silverlight MVVM binding seems not to work

Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't understand.
Having a WPF background i don't know what is going on here:
ViewModel has several properties, in which one is called SelectedRecord. This is a get only property and is defined like this:
public Culture SelectedRecord {
get { return culturesView.View.CurrentItem as Culture; } }
As you can see it is gets the current value of a CollectionViewSource (called culturesView). So if i select a Culture, the SelectedRecord (gets a value directly from within the CollectionViewSource) as expected. (Actually there is a datagrid control bound to the CollectionViewSource, hence it is possible to change the selected item)
OK. Now to the View . There are several views which access this ViewModel and in particular there is one which shows the values of the aforementioned property SelectedRecord (let's call it the EditView). To show this EditView there is a button (which has its Command property bound to an ICommand in the ViewModel) which functions (the first time) as expected.
This means:
1st try : i select a record, switch to EditView, outcome: selected record values are shown (as expected!!).
2nd try: switch back to datagrid, select another record, switch to EditView, outcome: the values of the previous shown record are shown again!!! WHY??
First i thought that the SelectedRecord has not the correct value set, but i was mistaken: it HAS the correct value! So it should be shown!?
What am i missing? In WPF this would work!!
Thanks in advance
When CurrentItem value changes, your ViewModel that has SelectedRecord must call RaisePropertyChanged("SelectedRecord") so whatever View is bound to it is notified about the change.

Silverlight listbox won't update with new itemsource

I have a listbox which is bound to a generic list, whenever I removed an item from the generic list and rebind it to the listbox it still shows the deleted items. Here is the code :
InventoryList.Remove(currInv);
lstSubMenu.ItemsSource = InventoryList;
lstSubMenu.DisplayMemberPath = "InventoryItemName";
I checked the generic list and the item is being removed and there doesn't seem to be any errors in the output window.
Set the ItemsSource = null before you set it to be InventoryList.
However, it is generally better practice to set the ItemsSource property once and never again. You can do this by using an ObservableCollection. Once you do this you can add/remove to your heart's content and not have to worry about the binding target not getting updated.

(WPF) Binding two classes to a control

I have two viewmodels. One which displays a collection of IPAddresses, and one which displays a collection of objects that has numerous parameters. One of these parameters is an IPAddress. So, I have another panel that binds to the properties of the second object. I would like a combobox to have the ItemSource set to the first object, but the selected item bound to the second object. However, I can only seem to set one datacontext on a control in code behind. Is there any way around this? I would prefer to do this all in code behind if possible (i find the xaml programming to be non-ideal at best), but I'll take anything.
For the ComboBox bind the collection of IPAddresses to the ItemsSource property, and bind the SelectedItem of the ComboBox to the IPAddress property of the SelectedItem of the collection of "numerous property objects".
This would be easier to answer if I had a better description of your objects including names. But it sounds to me like you should make a dictionary with the ipaddress as the key and the second object as the value.
If you can do that then you can bind to it in code like so:
comboBox.ItemsSource = dictionary;
comboBox.DisplayMemberPath = "Key";
comboBox.SelectedValuePath = "Value";
This is assuming that you have exactly one "second object" for every IPAddress in the collection. Which sounds about right given your description.
Take a look at the Source property of Bindings. It is basically a DataContext for a specific binding. It should make what you're trying to do very simple, especially in code behind.

Resources