Hello,
Normal 0 21 false false false DE X-NONE X-NONE
Let me explain my issue.
We have a XamDataGrid which its DataSource is a binding to an ObservableCollection within a ViewModel (Farms). One of the fields of the XamDataGrid is an extended XamComboEditor with the properties SelectedValue and SelectedValuePath.
We bind the ComboEditor field using the ItemsSource property to another ObservableCollection (RegionItems) of another ViewModel. In this field our DisplayMemberPath is a name (Region) and the SelectedValuePath and ValuePath are the ID of the Region.
All of these works fine until we try to filter the result through the Region in the UI. In this case the ID is shown and not the name of the Region. Something similar happens when we generate a report. In the report it is shown the ID instead of the name of the element.
We don’t know the reason why this is happening. We’ve thought, we should use a ComboBoxItemsProvider for the XamComboEditor, but the same binding fails if we use the provider.
Here is some of the currently code:
<xamDataGridExt:XamDataGridMvvm Grid.Row="1" DataSource="{Binding Farms}" ActiveDataItem="{Binding SelectedItem}" Name="XamDataGridMvvm">
<igDP:Field Name="RegionFI" Label="Region"> <igDP:Field.Settings> <igDP:FieldSettings EditorType="{x:Type igEditorsExt:XamComboEditorExt}"> <igDP:FieldSettings.EditorStyle> <Style TargetType="{x:Type igEditorsExt:XamComboEditorExt}"> <Setter Property="DisplayMemberPath" Value="Region"/> <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}, Path=DataRecord.DataItem.RegionItems, Mode=OneWay}"/> <Setter Property="SelectedValuePath" Value="ID"/> <Setter Property="SelectedValue" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}, Path=DataRecord.DataItem.RegionFI}"/> <Setter Property="ValuePath" Value="ID"/> </Style> </igDP:FieldSettings.EditorStyle> </igDP:FieldSettings> </igDP:Field.Settings></igDP:Field>
This new code doesn't work:
<igDP:XamDataGrid.Resources> <igEditors:ComboBoxItemsProvider x:Key="RegionsItemsProvider" DisplayMemberPath="Region" ValuePath="ID" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}, Path=DataRecord.DataItem.RegionItems, Mode=OneWay}" /> <Style x:Key="RegionFieldStyle" TargetType="{x:Type igEditors:XamComboEditor}"> <Setter Property="ItemsProvider" Value="{StaticResource RegionsItemsProvider}" /> </Style> </igDP:XamDataGrid.Resources>
<igDP:Field Name="RegionFI" Label="Region}"> <igDP:Field.Settings> <igDP:FieldSettings EditorType="{x:Type igEditorsExt:XamComboEditorExt }" > <igDP:FieldSettings.EditorStyle> <Style TargetType="{x:Type igEditorsExt:XamComboEditorExt}"> <Setter Property="ItemsProvider" Value="{DynamicResource RegionsItemsProvider}" /> </Style> </igDP:FieldSettings.EditorStyle> </igDP:FieldSettings> </igDP:Field.Settings> </igDP:Field>
If we execute this code we become the following in the Immediate Window, although the ItemsSource in both cases is the same:
System.Windows.Data Error: 4: Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Infragistics.Windows.DataPresenter.DataRecordPresenter', AncestorLevel='1''. BindingExpression:Path=DataRecord.DataItem.RegionItems; DataItem=null; target element is 'ComboBoxItemsProvider' (HashCode=64360188); target property is 'ItemsSource' (type 'IEnumerable')
We would be grateful for any help.
Thanks,
Chema
Hello Chema,
I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.
Hi Petar,
Thank you for your answer!!
In order to achieve it we just changed our property to return the name and not the ID and appartently it works. We changed the ValuePath to "Region" but then the ID it's shown in the DataGrid.
I tried your solution and unfortunately it does not work yet. Our RegionFI is a foreign key to the table Region and therefore of type int. And in this case, the data is not properly presented. If we change the ValuePath to ID (as we have in our project), the data is good, but not the filter.
I send you the sample project back with the tiny modification.
Thanks!
Hi Chema,
Excuse me for the late reply. I have been looking into this and frankly I am having a hard time understanding what the overall goal is here. Would you please summarize what the end result should be in the sample application you have reattached.
Thanks in advance. Looking forward to hearing from you.
What I try to achieve is to see in the filter of the Region the name of the region ("region1" for instance) instead of its id ("1").
The filter options should be the names of the regions and not their id's.
Thanks.
So you want the values to be 1,2,3 and at the same time they should appear in the combo as Region 1, Region 2 and Region 3 and also in the filter drop down they should also appear as Region 1, Region 2 and Region 3? Is that right?
Yes, that's right!! We want to show the friendly name of the region to the user but internally we need to work with the id.
I have been looking into this and in order to keep, both the FilterDropDown as the FilterCellValuePresenter’s editor and the original values in the data I can suggest registering for the RecordFilterDropDownOpening event where you can change the items’ DisplayText like so:
private void xamDataGrid1_RecordFilterDropDownOpening(object sender, RecordFilterDropDownOpeningEventArgs e)
{
foreach (FilterDropDownItem item in e.DropDownItems)
if (item.IsCellValue)
// ADD DisplayText logic here
item.DisplayText = "region " + item.Value.ToString();
}
Another thing you can try is convert the values from your data into the desired strings before they enter the XamDataGrid’s cells, after which you can use only the string values inside the XamDataGrid and convert them back when changes are made to the data. In order to illustrate this approach I had to modify a few things on the original sample. I have reattached the new version (WpfApplication1_modified.zip)
Please let me know if I can be of any further assistance whatsoever.