我正在处理一个数据集,它使用户能够选择多个行。但是,当用户单击行的标题时,所选内容就会丢失。
如果行已经被选中,如何在右击中禁用行选择。
我试图通过行为来做到这一点
public class DataGridRowBehavior
{
public static readonly DependencyProperty DisableSelectionOnRightClickProperty = DependencyProperty.RegisterAttached(
"DisableSelectionOnRightClick",
typeof(bool),
typeof(DataGridRowBehavior),
new UIPropertyMetadata(false, OnDisableSelectionOnRightClick));
public static bool GetDisableSelectionOnRightClick(DependencyObject dgRow)
{
return (bool)dgRow.GetValue(DisableSelectionOnRightClickProperty);
}
public static void SetDisableSelectionOnRightClick(DependencyObject dgRow, bool value)
{
dgRow.SetValue(DisableSelectionOnRightClickProperty, value);
}
public static void SetListViewFocus(DependencyObject d, bool use)
{
d.SetValue(DisableSelectionOnRightClickProperty, use);
}
public static void OnDisableSelectionOnRightClick(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DataGridRowHeader header = d as DataGridRowHeader;
header.MouseRightButtonUp += header_MouseRightButtonUp;
}
static void header_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var header = sender as DataGridRowHeader;
if (header.IsRowSelected)
{
if (header.ContextMenu != null)
{
header.ContextMenu.IsOpen = true;
}
e.Handled = true;
}
}
}但是这个功能不能正常工作,因为右击的其他功能也被破坏了。例如上下文菜单。上下文菜单没有启用其应用程序命令。
如果我单击所选行中的任何一行,是否有其他方法禁用所选内容或让所选内容保持不变?
发布于 2014-09-02 14:58:28
你有两个选择:
DataGrid或DataGridRow,并创建SelectionChanging或Selection事件。这是需要防止选择的。这一次,此控件只有SelectionChanged和Selected事件。下一次,我想你可以写代码Behavior。例如:
公共类AssociatedObject.AddHandler(UIElement.PreviewMouseLeftButtonDownEvent,SuppressButtonClickBehavior :行为{受保护覆盖OnAttached() { base.OnAttached();AssociatedObject.RemoveHandler(UIElement.PreviewMouseLeftButtonDownEvent,新RoutedEventHandler(OnPreviewMouseLeftButtonDown));}私有OnPreviewMouseLeftButtonDown(对象发送方,RoutedEventArgs e) { e.Handled = true;if (AssociatedObject.Command != null) { AssociatedObject.Command.Execute(AssociatedObject.CommandParameter);}}如果您愿意,可以使此代码更加灵活。但您必须了解,您只能将e.Handled设置为true以防止选择。
https://stackoverflow.com/questions/23508427
复制相似问题