在WPF/MVVM中使用DataGridRowEventArgs [英] Using DataGridRowEventArgs in WPF/MVVM
本文介绍了在WPF/MVVM中使用DataGridRowEventArgs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataGrid,并且当用户添加行时,我想自动滚动到最后一行.我的解决方案是,我使用LoadingRow事件.但是我的问题是,我不知道如何在事件中使用数据网格.
I have a DataGrid and I want to scroll automatic to my last row, when the user added rows. My Solution is, that i use the LoadingRow event. But my Problem is, that I don't know, how i use my datagrid in the event.
XAML代码:
<i:Interaction.Triggers>
<i:EventTrigger EventName="LoadingRow">
<cmd:InvokeCommandAction Command="{Binding LoadingRowCommand}"></cmd:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
ViewModel-Constructor:
ViewModel-Constructor:
public MainWindowViewModel()
{
LoadingRowCommand = new DelegateCommand<DataGridRowEventArgs(LoadingRow, CanExecute);
}
ViewModel中的LoadingRowEvent:
LoadingRowEvent in ViewModel:
public DelegateCommand<DataGridRowEventArgs> LoadingRowCommand { get; set; }
private bool CanExecute(DataGridRowEventArgs e)
{
return true;
}
private void LoadingRow(DataGridRowEventArgs e)
{
var currentRow = e.Row.Item;
datagrid.ScrollIntoView(currentRow); // <- here is my Problem, how do get my datagrid?
}
推荐答案
附加属性如下
namespace WPFGridLoadingRow
{
public class DataGridExtension
{
public static readonly DependencyProperty RowLoadedCommandProperty = DependencyProperty.RegisterAttached(
"RowLoadedCommand",
typeof(ICommand),
typeof(DataGridExtension),
new PropertyMetadata(null, OnRowLoadedcommandChanged));
private static void OnRowLoadedcommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = d as DataGrid;
if (dataGrid == null)
return;
if (e.NewValue is ICommand)
{
dataGrid.LoadingRow += DataGridOnLoadingRow;
}
}
private static void DataGridOnLoadingRow(object sender, DataGridRowEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (dataGrid == null)
return;
ICommand rowLoadedCommand = GetRowLoadedCommand(dataGrid);
rowLoadedCommand.Execute(e.Row.Item);
// you can also pass the complete row. Or you move your complete ui-logic here to this attached property.
}
public static void SetRowLoadedCommand(DependencyObject element, ICommand value)
{
element.SetValue(RowLoadedCommandProperty, value);
}
public static ICommand GetRowLoadedCommand(DependencyObject element)
{
return (ICommand)element.GetValue(RowLoadedCommandProperty);
}
}
}
用法:
View.xaml:
View.xaml:
<Window x:Class="WPFGridLoadingRow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFGridLoadingRow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:VM x:Key="ViewModel"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource ViewModel}}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding ItemsSource}" Name="dgGrid" AutoGenerateColumns="True" local:DataGridExtension.RowLoadedCommand="{Binding RowLoadedCommand}" >
</DataGrid>
<Button Command="{Binding AddRowCommand}" Grid.Row="1" >
Add row
</Button>
</Grid>
</Window>
ViewModel:
ViewModel:
namespace WPFGridLoadingRow
{
[ImplementPropertyChanged]
public class VM: ViewModelBase
{
public ObservableCollection<MyLong> ItemsSource { get; set; }
public VM()
{
ItemsSource = new ObservableCollection<MyLong>();
AddRowCommand = new DelegateCommand(AddRow);
RowLoadedCommand = new DelegateCommand<object>(RowLoaded);
}
public void RowLoaded(object currentRow)
{
// currentRow now is the same as e.Row.Item
}
public DelegateCommand AddRowCommand { get; set; }
private void AddRow()
{
ItemsSource.Add(new MyLong { Value = 333L });
}
public DelegateCommand<object> RowLoadedCommand { get; set; }
}
[ImplementPropertyChanged]
public class MyLong
{
public long Value { get; set; }
}
}
这篇关于在WPF/MVVM中使用DataGridRowEventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文