Ste*_*pUp 4 c# wpf event-handling mvvm
我尝试以明显的方式处理路由ScrollViewer.ScrollChanged事件DataGrid:
<i:Interaction.Triggers>
<i:EventTrigger EventName="ScrollViewer.ScrollChanged">
<ei:CallMethodAction MethodName="ScrollChangedHandler" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
但ScrollChangedHandler连开枪都没有。
然后,我找到了这篇关于处理事件的文章,但我无法弄清楚 xml namespace( xmlns) 的用途mvvmjaco:
<Image Width="360" Height="177" Source="Resources\PlayerArea.png">
<i:Interaction.Triggers>
<mvvmjoy:RoutedEventTrigger RoutedEvent="s:Contacts.ContactDown">
<mvvmjaco:CommandAction Command="{Binding TouchCommand}" />
</mvvmjoy:RoutedEventTrigger>
</i:Interaction.Triggers>
</Image>
Run Code Online (Sandbox Code Playgroud)
mvvmjoy使用文章中的此类:
public class RoutedEventTrigger :EventTriggerBase<DependencyObject>
{
RoutedEvent _routedEvent;
//The code omitted for the brevity
}
Run Code Online (Sandbox Code Playgroud)
基本上,我有两个问题:
mvvmjaco我应该为xml 命名空间使用什么类或库?ScrollViewer.ScrollChanged其参数处理 viewModel 中的事件?我会用以下附加属性来解决它:
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication2
{
public class DataGridExtensions
{
public static readonly DependencyProperty ScrollChangedCommandProperty = DependencyProperty.RegisterAttached(
"ScrollChangedCommand", typeof(ICommand), typeof(DataGridExtensions),
new PropertyMetadata(default(ICommand), OnScrollChangedCommandChanged));
private static void OnScrollChangedCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = d as DataGrid;
if (dataGrid == null)
return;
if (e.NewValue != null)
{
dataGrid.Loaded += DataGridOnLoaded;
}
else if (e.OldValue != null)
{
dataGrid.Loaded -= DataGridOnLoaded;
}
}
private static void DataGridOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
DataGrid dataGrid = sender as DataGrid;
if (dataGrid == null)
return;
ScrollViewer scrollViewer = UIHelper.FindChildren<ScrollViewer>(dataGrid).FirstOrDefault();
if (scrollViewer != null)
{
scrollViewer.ScrollChanged += ScrollViewerOnScrollChanged;
}
}
private static void ScrollViewerOnScrollChanged(object sender, ScrollChangedEventArgs e)
{
DataGrid dataGrid = UIHelper.FindParent<DataGrid>(sender as ScrollViewer);
if (dataGrid != null)
{
ICommand command = GetScrollChangedCommand(dataGrid);
command.Execute(e);
}
}
public static void SetScrollChangedCommand(DependencyObject element, ICommand value)
{
element.SetValue(ScrollChangedCommandProperty, value);
}
public static ICommand GetScrollChangedCommand(DependencyObject element)
{
return (ICommand)element.GetValue(ScrollChangedCommandProperty);
}
}
}
Run Code Online (Sandbox Code Playgroud)
该类UIHelper看起来像:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace WpfApplication2
{
internal static class UIHelper
{
internal static IList<T> FindChildren<T>(DependencyObject element) where T : FrameworkElement
{
List<T> retval = new List<T>();
for (int counter = 0; counter < VisualTreeHelper.GetChildrenCount(element); counter++)
{
FrameworkElement toadd = VisualTreeHelper.GetChild(element, counter) as FrameworkElement;
if (toadd != null)
{
T correctlyTyped = toadd as T;
if (correctlyTyped != null)
{
retval.Add(correctlyTyped);
}
else
{
retval.AddRange(FindChildren<T>(toadd));
}
}
}
return retval;
}
internal static T FindParent<T>(DependencyObject element) where T : FrameworkElement
{
FrameworkElement parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
return FindParent<T>(parent);
}
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的定义中写下DataGrid:
<DataGrid ItemsSource="{Binding MySource}" extensionsNamespace:DataGridExtensions.ScrollChangedCommand="{Binding ScrollCommand}"/>
Run Code Online (Sandbox Code Playgroud)
在你的 ViewModel 中你有一个ICommand看起来像这样的:
private ICommand scrollCommand;
public ICommand ScrollCommand
{
get { return scrollCommand ?? (scrollCommand = new RelayCommand(Scroll)); }
}
private void Scroll(object parameter)
{
ScrollChangedEventArgs scrollChangedEventArgs = parameter as ScrollChangedEventArgs;
if (scrollChangedEventArgs != null)
{
}
}
Run Code Online (Sandbox Code Playgroud)
对于您的第一个问题(特别感谢安迪·奥尼尔和马格努斯·蒙廷):
MVVMJaco是 xmlns:mvvmjaco="galasoft.ch/mvvmlight" 所需的库是:
| 归档时间: |
|
| 查看次数: |
7382 次 |
| 最近记录: |