Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1915
Handling XamDataGrid filter value in MVVM friendly way
posted

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}}