I try to bind my XamComboEditor's DataSource of my xamDataGrid and the binding doesn't work. The field is a lookup, where the user must pick a value from the combobox. The list contains Description and the bound field is called DrugId. DrugList.Model contains a Id and a Description. Here's my code:
<igDP:UnboundField Label="{x:Static cow:HealthResources.Drug}" Row="1" Column="0" BindingMode="TwoWay" BindingPath="DrugId">
<igDP:UnboundField.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}" >
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding DrugList.Model}" />
<Setter Property="DisplayMemberPath" Value="Description" />
<Setter Property="ValuePath" Value="Id" />
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:UnboundField.Settings>
</igDP:UnboundField>
This gives me the error in VS output window: BindingExpression path error: 'DrugList' property not found on 'object' ''TemplateDataRecord' (HashCode=3700058)'. BindingExpression:Path=DrugList.Model; DataItem='TemplateDataRecord' (HashCode=3700058); target element is 'XamComboEditor' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
A simple combobox like this show the items list correctly, so the problem lies only in the way I have to set the binding:
<ComboBox Width="100" DisplayMemberPath="Description" ItemsSource="{Binding DrugList.Model}" </ComboBox>
How should I set the binding to fill that xamComboEditor?
Fred.
Hello Fred,
The binding looks okay, but the exception shows that this property cannot be found. I think the problem is in the relative source. It defaults to the record and not to the underlying data item. Try using a binding expression like this:
DataItem.DrugList.Model to target the business object.
With DataItem.DrugList.Model, it now complains that DrugList is not part of my object that is binded to the grid's DataSource property. 'DrugList' property not found on 'object' ''AnimalTreatmentEntity' (HashCode=4154422)'.
That's a step forward, but my DrugList is not related in any way to the object I use as a DataSource. What else can I try?
Оh, okay.
In this case, you indeed need to set the RelativeSource to target the element that exposes the DrugList collection. How/where is the DrugList exposed?
The DrugList is a property of my ViewModel, just like my grid's DataSource object. They are declared at the same place.
Maybe it'll help you if I tell you that I also have a property binded to the grid's ActiveRecord and that works.
In my ViewModel:
public Record GridActiveRecord { get; set; }
public DrugListViewModel DrugList { get; set; }
public AnimalTreatmentEntityList Model {get; set;}
XAML binding that works:
<igDP:XamDataGrid Name="gridTreatments"
DataSource="{Binding Model}"
ActiveRecord="{Binding Path=GridActiveRecord, Mode=TwoWay}">
Inside the grid, ItemSource binding that won't work:
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Path=DataItem.DrugList.Model}" />
</EventSetter>
Thanks a lot for your detailed example. Your code works like a charm. Since my grid is in a UserControl, I changed it to x:Type UserControl and it worked.
Thanks again.
That is what I thought. As I said, you need to change the relative source. The reason why it works with the XamDataGrid's DataSource is because its DataContext is your ViewModel. However, the DataContext of the Editor is the Record, so you need to change that, to something like (assuming that you're setting it to the window: this.DataContext = new MainViewModel(); )
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.DrugList.Drugs, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
See the attached project.