The problem is when you scroll with the mouse wheel, the canExecute is not called.
I create a AttachedProperty to correct this, and it could be use in a style.
public static readonly DependencyProperty CommandRefreshOnScrollingProperty = DependencyProperty.RegisterAttached(
"CommandRefreshOnScrolling",
typeof(bool),
typeof(DataGridProperties),
new FrameworkPropertyMetadata(false, OnCommandRefreshOnScrollingChanged));
private static void OnCommandRefreshOnScrollingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var dataGrid = d as DataGrid;
if (dataGrid == null)
{
return;
}
if ((bool)e.NewValue)
{
dataGrid.PreviewMouseWheel += DataGridPreviewMouseWheel;
}
}
private static void DataGridPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
CommandManager.InvalidateRequerySuggested();
}
And you can use this attachedProperty in a style like this:
<Setter Property="views:DataGridProperties.CommandRefreshOnScrolling" Value="True"></Setter>
Thanks Eran Otzap to show me why i got this problem!