【发布时间】:2012-08-04 23:01:42
【问题描述】:
现在这里基本上有两个问题,让我轻轻地向您介绍一下我目前遇到的问题。假设我们有一个常规的 DataGrid,我尝试在行上应用 PreviewMouseRightButtonDown 以实现自定义功能,同时避免选择,因为这会扩展详细信息视图。我以为this post would help; it was directed at ListView, but with few adjustment it should work the same, right?
你为什么要这样做?,你可能会问。
我想避免在右键单击时打开详细信息,因为在主项目详细信息部分中(有时)会花费很长时间访问数据库,而右键单击只会在集合中的视图模型中设置适当的bool flag-property .
MainWindowView.xaml:
<DataGrid AutoGenerateColumns="False" RowDetailsVisibilityMode="VisibleWhenSelected">
<!-- Columns ommitted for brevity -->
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<!-- Since I'm using Caliburn Micro anyway, I'm going to redirect the event to view model. It doesn't really matter, issue exists with EventSetter too. -->
<Setter Property="cal:Message.Attach" Value="[Event PreviewMouseRightButtonDown] = [Action CheckItem($eventArgs, $source]"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
MainWindowViewModel.cs:
public void CheckItem(RoutedEventArgs args, object source)
{
var row = source as DataGridRow;
if (row != null)
{
var item = (ItemViewModel)row.Item;
item.IsChecked = true;
}
args.Handled = true;
}
提问时间:
- 为什么
RoutedEventArgs上的RoutingStrategy被列为Direct而不是Tunneling?我以为所有Preview事件都是Tunneling。
- 更重要的是:如果我在
CheckItem中放置一个断点,上述解决方案有效,选择不会发生并且详细信息已折叠,一切正常 如预期。如果我删除断点,则会选择项目并 详细信息部分打开,就好像事件没有停止一样 传播。为什么会这样?我以为设置RoutedEventArgs上的Handled到true应该只表明 事件确实得到处理。
[编辑]
现在我找到了一个“低俗”的解决方法,我可以附上PreviewMouseDown 事件:
bool rightClick;
public void MouseDown(object source, MouseEventArgs args)
{
rightClick = false;
if (args.RightButton == MouseButtonState.Pressed)
{
rightClick = true;
//do the checking stuff here
}
}
然后连接到SelectionChanged事件:
public void SelectionChanged(DataGrid source, SelectionChangedEventArgs args)
{
if (rightClick)
source.SelectedIndex = -1;
}
它适用于我的特殊情况,但主观上看起来很臭,所以我愿意接受任何其他建议。尤其是为什么简单的鼠标事件eventArgs.Handled = true 不足以抑制稍后触发SelectionChanged :)
【问题讨论】:
-
@Blam 事件触发,它本质上与使用
<EventSetter Event="PreviewMouseRightButtonDown" Handler="CheckItem"/>相同,但是这样你必须在视图后面的代码中处理事件,将事件附加到 Caliburn 附加的 Micro属性使您能够在视图模型中处理此问题。尽管如此,即使您使用EventSetter并在代码隐藏中执行所有操作,它仍然是相同的 - 事件通过,详细信息行打开。
标签: c# wpf wpfdatagrid routed-events