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?
In order to access the properties of the currently selected AccountAttribute of the combo editor for any record, I can suggest you introduce the same type of property (AccountAttribute) for the DataItem as well. This way we can bind the AccountAttribute of the DataItem to the respective AccountAttribute from the combo editor's ItemsSource.
public class Item{ ... public AccountAttribute AccountAttribute { get; set; }}
public AccountAttribute AccountAttribute { get; set; }}
<Style TargetType="igE:XamComboEditor"> .... <Setter Property="SelectedItem" Value="{Binding Path=DataItem.AccountAttribute}" /></Style>
Since the AccountAttribute property of the DataItem is of class type, we will need to use AlternateBinding and manually specify which value of the AccountAttribute we would like to visualize in the field.
<igDP:ComboBoxField Label="AccountAttribute" AlternateBinding="{Binding Path=AccountAttribute.AccountValue}">
We can now access the properties of the AccountAttribute for any DataItem (for example the SelectedDataItem of the XamDataGrid):
var accountId = (XDT.SelectedDataItem as Item).AccountAttribute.AccountId;var accountValue = (XDT.SelectedDataItem as Item).AccountAttribute.AccountValue;
Please note that when MVVM is used, the code-behind logic of the view is best to be kept view-specific.I have attached a sample application that demonstrates the approach from above.
Thank you!
Thank you for the feedback.
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.