I am using MVVM. My VM has two properties (1) [Data] which returns ObservableCollection<Account> and (2) [AccountAttributes] which returns ObservableCollection<AccountAttribute>.
I View's code behind DataContext set to above class. [Data] is bound to xamDataGrid and it works fine. I want to bind an property in [AccountAttributes] to XamComboEditor which is linked to a column in xamDataGrid. What is the exact syntax in XAML? If I change [AccountAttributes] to List<string> it works fine but want it to be an ObservableCollection.
Thanks.
Hello Jay,
Thank you for the implementation description you have provided.
Presuming that you would like to bind an ObservableCollection of class instances to the XamComboEditor when using MVVM, you can specify the display and value values that will be used for the binding. (DisplayMemberPath and ValuePath properties of the editor)
C#:
public class AccountAttribute{ public int AccountValue { get; set; }} public ObservableCollection<AccountAttribute> AccountAttributes { get; set; }
public ObservableCollection<AccountAttribute> AccountAttributes { get; set; }
XAML:
<Style TargetType="igE:XamComboEditor"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.AccountAttributes, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" /> <Setter Property="ValuePath" Value="AccountValue" /> <Setter Property="DisplayMemberPath" Value="AccountValue" /></Style>
If you have any questions, please let me know.
Thanks, your syntax works perfect.
One more help please: I have a couple of events on the grid like xgrdMain_RecordUpdated. How do I access the SelectedItem of the above combo-box in code behind?
In your example above, I get the AccountValues in the drop-down and in the associated grid column but I also want to know the AccountId of the SelectedItem in code behind?
Thank you for the sample application you have provided.
I tested the behavior you have described inside the window's constructor and when the following lines are executed (they were uncommented by default), I was able to successfully add new records by using the AddNew record functionality at runtime.
/* * * UN-COMMENT THE NEXT TWO LINES AND YOU CAN NOT ADD NEW ROW IN THE GRID * */companies.Data.Add(new Company("Microsoft", "111"));companies.Data.Add(new Company("Intel", "222"));
You mentioned that a property of your DataItem is null when adding it to the XamDataGrid (presuming through the AddNew record). Whenever we type inside a cell of the AddNew record, the XamDataGrid automatically detects that and creates a new record and a new DataItem instance for it by using its parameterless constructor (the RecordAdded event is fired). Since the cell is still in edit mode at this time and the record has not been committed to the XamDataGrid yet, it is expected for the property that corresponds to this cell's value to return null. As soon as we leave the cell (for example enter into the next cell of the same record) or we commit the record, the setter of the property will trigger and the value will be set.The sample application you have provided does not use the XamComboEditor control and this is why I cannot refer to its behavior when using the data from this sample.
My apologies - it is when you COMMENT the above two lines, the ObservableCollection is empty and I am not able to add new rows. Please try it.
As for the XamComboEditor issue, I haven't yet got to adding that in my app. Will do so and post later.
Thank you for the feedback.
I tested the application by commenting these couple of lines as well and I was still able to successfully add new items to the XamDataGrid and its underlying DataSource (the only difference is that the XamDataGrid had no data initially, which is expected).
I have attached a sample application that shows the content of the ObservableCollection on a button click. (refer to the screenshot.png image inside the img folder of the zip)
I am glad to know you were able to achieve the desired functionality.
Hi - I need to check on this w.r.t my app. I thing your code should work. I will post in update when I get back to that section of my app. Thanks.
I presume you are referring to the SelectedItem property of the XamComboEditor, since the ComboBoxField does not have such property.
I tested the behavior you have described in regards to adding a new record and not having the AccountAttribute property of the DataItem set and I was not able to reproduce it.Both when adding a record through the UI (the AddNew record) and from code-behind (for testing purpose), the AccountAttribute of the DataItem is successfully set.When working with class types (such as AccountAttribute), in order to compare different instances, it is good for us to override the Equals and GetHashCode methods. This way we will be able to compare the objects by their content or as we desire.
public class AccountAttribute{ ... public override bool Equals(object obj) { var other = obj as AccountAttribute; if (other == null) return false; return this.AccountValue.Equals(other.AccountValue) && this.AccountId.Equals(other.AccountId); } public override int GetHashCode() { return base.GetHashCode(); }}
public override bool Equals(object obj) { var other = obj as AccountAttribute; if (other == null) return false; return this.AccountValue.Equals(other.AccountValue) && this.AccountId.Equals(other.AccountId); }
public override int GetHashCode() { return base.GetHashCode(); }}
I have attached a sample application that demonstrates the approach from above.
Well, the other pending issue is with using property SelectedItem of the ComboBoxField.
Binding SelectedItem to DataItem.AccountAttribute works. However, when I add a new row, the selected AccountAttribute from the combo-box (the combo is populated with a collection of AccountAttribute) is not saved to the AccountAttribute property of the underlying DataItem.
I don't mind using code-behind to do this.
In code-behind if I could just say:
Item.AccountAttribute = <the AccountAttribute selected in the combo-box> // this is not happening automatically !!!
Or something like
Item.AccountAttribute = MyCombo.SelectedItem As AccountAcctribute
I believe this thread can help other people looking for a similar solution.