I am using XamDataGrid in an MVVM application and trying to figure out the recommended way to handle grid filter value/selection change in the ViewModel. So I created an attached property that handles RecordFilterChanged event as shown below. When I modify a filter value on the grid I see that the property RecordFilterChangedCommandChanged never gets executed. Am I handling the wrong event here or something else is wrong?
C#:
public static readonly DependencyProperty RecordFilterChangedCommandProperty = DependencyProperty.RegisterAttached("RecordFilterChangedCommand", typeof(ICommand), typeof(XamDataPresenter), new FrameworkPropertyMetadata((string)null, RecordFilterChangedCommandChanged));
public static void SetRecordFilterChangedCommand(DataPresenterBase grid, ICommand command)
{ grid.SetValue(RecordFilterChangedCommandProperty, command);}
public static ICommand GetRecordFilterChangedCommand(DataPresenterBase grid)
{ return grid.GetValue(RecordFilterChangedCommandProperty) as ICommand;
}
private static void RecordFilterChangedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ DataPresenterBase presenter = (DataPresenterBase)d; presenter.RecordFilterChanged += (o, ea) =>{
ICommand command = (ICommand)presenter.GetValue(RecordFilterChangedCommandProperty); object commandParameter = presenter.GetValue(RecordFilterChangedCommandParameterProperty);
if(command.CanExecute(commandParameter))command.Execute(commandParameter);
};
Xaml:
b:XamDataPresenter.RecordFilterChangedCommand="{Binding RecordFilterChanged, RelativeSource={RelativeSource Self}}"
b:XamDataPresenter.RecordFilterChangedCommandParameter="{Binding ActiveDataItem, RelativeSource={RelativeSource Self}}
Hello Andre,
Thank you for your post. I have been looking into it and after further research I can suggest to create a Behavior, that would manage the command. I have created a small sample to show you this functionality.
Please find the attached sample application and feel free to let me know if you have any further questions on the matter.
Thanks for your suggestion, but I found a problem with my code and this particular question has been answered. However, I have a followup question - how do I set a 'default' filter value for a grid filter row/column? I attempted to use the WPF binding but I get an exception that filter property Value cannot be set using binding. Is there another way to get this initialized? My case is - the grid opens up, data is supplied by the ViewModel, there is a default value for some filter field that I want to be selected in the drop down filter or just showing once the grid is loaded. Thanks.
I have been looking into your issue and I can suggest to take a look at the following link from our documentation, where you can find more information regarding the scenario you have mentioned: http://help.infragistics.com/Help/NetAdvantage/WPF/2013.1/CLR4.0/html/xamDataPresenter_Add_Filter_Conditions.html.
Please do not hesitate to let me know if you have any further questions on this matter.
The code in the article sets the filter Value directly to a constant as shown below:
<igDP:FieldLayout> <igDP:FieldLayout.RecordFilters> <igDP:RecordFilter FieldName="department"> <igDP:RecordFilter.Conditions> <igWindows:ComparisonCondition Operator="Contains" Value="Sales" /> </igDP:RecordFilter.Conditions> </igDP:RecordFilter> </igDP:FieldLayout.RecordFilters> </igDP:FieldLayout>
In my case, however, I need to set the Value dynamically - the actual value should come from the ViewModel for the view. As I have mentioned before, WPF data binding for the Value throws exception (its not allowed) and no code behind is allowed in MVVM applications. So the question remains - how do I initialize default filter selections when the screen opens up without hardcoding the values? Thanks.
Hello Travis,
I have been looking into your issue and can suggest to take a look at the following forum thread, where a similar functionality is discussed: http://www.infragistics.com/community/forums/t/28156.aspx. Please do not hesitate to let me know if you have any further questions on the matter.