Referencing ItemSource in code - silverlight-4.0

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>;

Related

Listbox DisplayMember from nested object VB.net

I have a class called ComponentTransactionPair, it houses two objects called m_Component and m_Transaction, there is a public property
Protected m_componentTransactionPair As List(Of ComponentTransactionPair)
In trying to hook this to a ListBox I am using the code below
lstbCurrentTransactions.DataSource = m_componentTransactionPair
m_currentOptionsLoaded = True
lstbCurrentTransactions.DisplayMember = "m_Transaction.Description"
The DisplayMember is not working properly for me in doing this and i'm pretty sure it's displaying the type rather than the Description property within the Transaction.
Is it even possible to use a nested object's property to get this category here because everything I've seen does not use a nested object.
Fixed this by just adding another property to the class and took it from the value I needed. Worked perfectly afterwards. If there's a less hacky solution i'd be interested to hear it though.

How to Populate a List<object> in XAML?

I came to know the same thing for string using the link How to Populate a List<string> in XAML?
But need to do the same on object (say for string only)..
Use the binding context to populate the list. Set the binding target as list object and binding source as the source of data.
Look here for example. I think it answers your question.

How to bind multiple property in xaml for label control in Xamarin

I have two property TotalWeightInGrams and TotalWeightInKiloGrams, i want to bind in label control as following output
string format="{0} ({1} Kgs)"
Easiest way would be to make a new property that returns the string you want and bind to that. This way you don't need to do anything more complicated and can change the formatting in code if needed.

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.

Silverlight 4 MVVM ComboBox data binding is not being displayed

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.