Silverlight 4 MVVM ComboBox data binding is not being displayed - xaml

I have a ComboBox with the following XAML
<ComboBox Name="CompanyComboBox"
ItemsSource="{Binding Path=CompanyList, Mode=OneWay}"
SelectedItem="{Binding Path=CurrentCompany, Mode=TwoWay}"
DisplayMemberPath="Name" />
Problem:
The selected option on 'company' is persisted, but never gets displayed on load. What's missing or going wrong, or what have I forgotten to do?
CompanyList has data, and the ComboBox does get populated
The selection on the ComboBox does save to the database via the TwoWayBinding
More code is on pastebin.com, the ViewModel and the Company class code.
I have tried the following suggestions, that have so far not solve the issue:
Two-way bind a combobox to a simple string array Order of ItemSource and SelectedValue properties on were correct
ComboBox.SelectedValue not updating from binding source alternating between 'SelectedValue' and 'SelectedIndex' - neither works
Silverlight 4 Combobox with selectedValue using MVVM-Light raising PropertyChanged before setting new value also didn't help
Adding/Removing 'IsEnabled="{Binding IsReady}' on the ComboBox didn't help either
Adding SelectedValuePath="Name" or ="Value" stopped the save from working

You need to overwrite the Company.Equals() method to return true if the object's data is the same.
By default, it only returns true if the two company objects being compared share the same spot in memory, and I am guessing that your CurrentCompany object does not point to an object in CompanyList, so the SelectedItem is being set to null

Check that the instance assigned to CurrentCompany is the actual one contained in CompanyList and not a duplicate of it.

You can try this:
after populating CompanyList in your ViewModel, set the CurrentCompany to the first company, or a dummy item that says , or depending on your context.

Can we take a look at the view model please? Until this information is not present, may give following suspections.
CurrentCompany property is not public or is not a property.
View model doesn't implement INotifyPropertyChanged interface.
Setter of CurrentCompany property doesn't contain PropertyChanged event notification.

Related

Create custom SelectedIndices collection from existing SelectedRows collection in datagridview

I need to write a custom collection where it will store the selectedindices of the datagridview. Sine we have SelectedRows colllection property already present in Datagridview. I want to use it and get indices collection so that I can I can create SelectedIndices as a custom property for my Datagridview which is of type collection. Can some one help m eon how will I do it.
For reference;
Selectedindices is a property of Listview provided by microsoft.I want the same with Datagridview. But since I don have that I want to use the SelectedRows property and an create a custom property Selectedindices for my datagridview. It should be of type Collection. PLease suggest if I can do the same .
I do believe that feature is already available, maybe you can find out more by view the link below?
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewselectedrowcollection.aspx
Also a little code sample of how far you got, or what you have tried already always entices other members to be more helpful.

Is it possible to bind control visibility to the content? (XAML)

I created a result view for a class, calculating some math-stuff. According to the values, set by the user, there are some of the results returning 0 or "". So some of the Controls bound to this empty properties are also empty. Thats some kind of ugly, because calculating with these Values will never contain such a result. So I don't want to show this control.
Is it possible, not to show this control if the property it is bound to is empty?
Greetings from Germany
Bind Value to element Visibility property with help of Converter that will provide your logic.

Referencing ItemSource in code

I have a SL4 DataGrid whose ItemsSource is set to a StaticResource pointed to a ViewModel.
I need to get the collection of data items from the DataGrid in a code-behind file.
Since the ItemsSource is an IEnumerable, I thought this would work, but it does not.
IEnumerable listEntities = DataGrid.ItemsSource as MyEntity;
I'm sure that I just have some simple construct wrong. Any help is appreciated.
Thanks.
Did you tried IEnumerable listEntities = DataGrid.ItemsSource as IEnumerable<MyEntity>;

Silverlight 4 ComboBox - Binding to Nullable data (tried TargetNullValue but not working as expected)

(Please note - I am a Silverlight beginner and am looking for the simplest solution here, e.g. that doesn't involve writing/installing a replacement for the ComboBox control!)
This is an issue with a Silverlight 4 application that uses the View Model (MVVM) approach. I have a simple form for editing a "Product" object. Product has a CategoryID property which is nullable (int?). A ComboBox is used to view and set the CategoryID - this is bound to an ObservableCollection of Categories. Product also has number of non-nullable properties bound to TextBoxes.
I want the user to see "N/A" in the ComboBox for a product with no category, and to be use this "N/A" option to set CategoryID to null. So, I manually added a Category object with CategoryID=0 and CategoryName="N/A" to the collection; then I set TargetNullValue=0 in the SelectedValue Binding of the ComboBox. My thinking was - when the ComboBox SelectedValue was bound to a null CategoryID it would substitute zero, and therefore select the "N/A" option.
When editing a Product with a non-null CategoryID, everything works. However when a null CategoryID is found, two problems occur:
No option is selected in the ComboBox (its blank)
The ComboBox binding seems broken from this point onwards - any Product I subsequently edit (incl. ones with a non-null CategoryID) have nothing selected in the ComboBox (its still populated with all categories - just no selected item).
I've seen reports of problem #2 (here, here) but I was under the impression that #1 should have worked.
What am I missing to get the "N/A" option to be selected?
XAML for ComboBox:
<ComboBox x:Name="cboCategory" ItemsSource="{Binding colCategories, Mode=OneWay}" SelectedValuePath="CategoryID" DisplayMemberPath="CategoryName" SelectedValue="{Binding CurrentProduct.CategoryID, Mode=TwoWay, TargetNullValue=0}" Height="24" Width="344"></ComboBox>
I ended up using a Converter that converts the Null in the bound object to zero in the ComboBox and then back again ... like the one mentioned here http://forums.silverlight.net/forums/t/195627.aspx

Autocompletebox binding problem

I have an autocomplete box where I am trying to bind the selected item to a propery of the item that is actually selected.
I.e. I have a Client object with a Name property, and the ItemsSource of my autocomplete box is a List of Clients.
The property I am trying to bind as the selected Item is a String, called SelectedClientName.
But because SelectedClientName is not of type Client, I cannot bind it directly.
As a work around, I am using a converter.
But I would like to know if there is a way to do this in the binding without converters.
I'd suggest having SelectedClientName not be of a different type, in your case of type string. Keep it the Client type and utilize your Name property where a string is required.
Here's some additional information on binding to complex objects in an AutoCompleteBox: http://www.codeproject.com/Tips/79158/AutoCompleteBox-Binding-Custom-Objects