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!
Thank you for the feedback.
IN your code,
if (XDT.SelectedDataItem == null) return;
always seems to equate to null. Can you please check. (FYI, I had to demote references to v16.2).
I tested the behavior you have described (with 16.2) in regards to the SelectedDataItem property always being null and I was not able to reproduce it. Only if no record is currently selected, the SelectedDataItem will be null.
If you have integrated the code from the sample within your application, would you please make sure you have also set the SelectedDataItemsScope property of the XamDataGrid to RecordsOrCells.
Okay, in my application, it picks up the correct Item.AccountAttribute for existing rows (i.e. binding seems to work). However, when I add a new row, it passes a null for DataItem.AccountAttribute. It does not save the selected AccountAttribute to Items.AccountAttribute in VM.
Also, this may be related: I have child xamDataGrid bound to an ObervableCollection and the Get{} accessor has following:
return new ObservableCollection<Account>(_data.Where(p => p.AccountId == _accountId));
when _accountId = 0, the above would return an empty collection. When this happens, I am not able to add new row.
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.