Tan*_*Tan 5 wpf ui-virtualization wpfdatagrid icommand
我正在使用框架.NET 4.0的WPF应用程序
我有一个DataGrid的问题:每行有2个命令:
public ICommand MoveUpOrderPipeCommand
{
get
{
if (_moveUpOrderPipeCommand == null)
{
_moveUpOrderPipeCommand = new Command<OrderPipeListUIModel>(OnMoveUpOrderPipe, CanMoveUpOrderPipe);
}
return _moveUpOrderPipeCommand;
}
}
private bool CanMoveUpOrderPipe(OrderPipeListUIModel orderPipe)
{
if (OrderPipes == null || !OrderPipes.Any() || OrderPipes.First() == orderPipe)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
MoveDown有相同的命令(可以执行检查该行是否不是最后一行)
和DataGrid:
<DataGrid Grid.Row="1" IsReadOnly="True" ItemsSource="{Binding OrderPipes}" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTextColumn Header="Diam. (mm)" Binding="{Binding Diameter}" Width="120"> </DataGridTextColumn>
<DataGridTextColumn Header="Lg. (m)" Binding="{Binding Length}" Width="120"></DataGridTextColumn>
<DataGridTextColumn Header="Ep. (mm)" Binding="{Binding Thickness}" Width="120"></DataGridTextColumn>
<DataGridTextColumn Header="Ondulation" Binding="{Binding Ripple}" Width="120"></DataGridTextColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.MoveUpOrderPipeCommand}" CommandParameter="{Binding}">
</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
如果我使用EnableRowVirtualization将我的网格虚拟化为真,我有一些麻烦,如果我滚动到底部(第一行不再可见)然后滚动回到顶部,有时第一行的按钮移动(通常不能向上移动) )启用,直到我点击DataGrid,并且第二个或第三个是禁用,应启用!
如果我将EnableRowVirtualization设置为false,我没有这个问题...
我只在互联网上发现了另一篇关于这个问题的帖子,但是没有来自.net框架的dataGrid:http: //www.infragistics.com/community/forums/t/15189.aspx
你知道我该怎么办呢?
先感谢您
编辑:命令类
public class Command<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
public Command(Action<T> execute) : this(execute, null)
{
}
public Command(Action<T> execute, Func<T, bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute", "Le délégué execute ne peut pas être nul");
this._execute = execute;
this._canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
public bool CanExecute(object parameter)
{
return (_canExecute == null) ? true : _canExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当您用鼠标滚轮滚动时,不会调用 canExecute。
我创建了一个 AttachedProperty 来纠正这个问题,它可以在一个样式中使用。
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();
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用这个attachedProperty:
<Setter Property="views:DataGridProperties.CommandRefreshOnScrolling" Value="True"></Setter>
Run Code Online (Sandbox Code Playgroud)
感谢 Eran Otzap 告诉我为什么我会遇到这个问题!
| 归档时间: |
|
| 查看次数: |
534 次 |
| 最近记录: |