简体   繁体   English

如何在xaml中的复选框对象上禁用dash + equals?

[英]How do I disable dash + equals on a check box object in xaml?

I have a datatemplate xaml that currently looks like this for a report screen 我有一个datatemplate xaml,目前在报告屏幕上看起来像这样

   <CheckBox>
        <StackPanel>
            <TextBlock Text="{Binding Path=DisplayName}" FontWeight="Bold" Margin="0 0 0 5" />
            <RadioButton Content="All Pages" IsChecked="{Binding Path=AllPages}" Margin="0 0 0 5" />
            <RadioButton>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Pages: " Margin="0 3 5 0" />
                    <TextBox Width="130" Text="{Binding Path=SelectedValue}" />
                </StackPanel>
            </RadioButton>
        </StackPanel>
  </CheckBox>

I want to know, how I can disable the default checkbox behavior that uses '-' and '=' as chek and uncheck. 我想知道,我如何禁用使用' - '和'='作为chek并取消选中的默认复选框行为。

I want to allow the user in that textbox to type in '1-5, 6, 7' for page ranges, but cannot do so because '-' is checking and unchecking the box 我想允许该文本框中的用户为页面范围键入“1-5,6,7”,但不能这样做,因为' - '正在检查并取消选中该框

EDIT: I would like to maintain the spacebar functionality of checking + unchecking the checkbox. 编辑:我想保持空格键功能检查+取消选中复选框。 For whatever reason, if I am in the textbox and I type space, it doesn't trigger the toggle event, but the '-' and '=' do. 无论出于何种原因,如果我在文本框中并输入空格,它不会触发切换事件,但是' - '和'='会触发。

EDIT2: Ideally looking for a xaml fix since I am trying to maintain an MVVM architecture and would prefer not to have code behind EDIT2:理想情况下寻找xaml修复程序,因为我正在尝试维护MVVM体系结构,并且不希望背后有代码

CheckBox.OnKeyDown is the culprit (see http://msdn.microsoft.com/en-us/library/system.windows.controls.checkbox.onkeydown.aspx ). CheckBox.OnKeyDown是罪魁祸首(请参阅http://msdn.microsoft.com/en-us/library/system.windows.controls.checkbox.onkeydown.aspx )。 I solved this by making a custom CheckBox class that overrides OnKeyDown and does nothing. 我通过创建一个覆盖OnKeyDown并且什么都不做的自定义CheckBox类来解决这个问题。 The VB solution would look like: VB解决方案看起来像:

Public Class IgnoreKeyboardCheckBox
    Inherits CheckBox

    Protected Overrides Sub OnKeyDown(e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.Space Then
            MyBase.OnKeyDown(e)
        End If
    End Sub
End Class

You can use this class instead of CheckBox whenever you need the checkbox to ignore keyboard inputs. 只要您需要复选框忽略键盘输入,就可以使用此类而不是CheckBox。

As can be seen here CheckBox.cs source code in C# .NET , for 2-state checkboxes there is custom behaviour for Key.Add, Key.Subtract, Key.OemPlus, Key.OemMinus . 从这里可以看出,C#.NET中的CheckBox.cs源代码 ,对于2状态复选框, Key.Add, Key.Subtract, Key.OemPlus, Key.OemMinus有自定义行为。

(Programmatically) adding commands didn't work for me. (以编程方式)添加命令对我不起作用。 Overriding in a subclass did: 覆盖子类时:

protected override void OnKeyDown(KeyEventArgs e)
{
  // Non ThreeState Checkboxes natively handle certain keys.
  if (SuppressNativeKeyDownBehaviour(e.Key))
    return;

  base.OnKeyDown(e);
}

private bool SuppressNativeKeyDownBehaviour(Key key)
{
  switch (key)
  {
    case Key.Add:
    case Key.Subtract:
    case Key.OemPlus:
    case Key.OemMinus:
      return true;
    default:
      return false;
  }
}

Try to override the keyboard shortcuts with empty commands: 尝试使用空命令覆盖键盘快捷键:

<RadioButton>
        <KeyBinding Key="OemMinus" Command="{Binding EmptyCommand}"/>
</RadioButton>

And in your viewmodel: 并在您的viewmodel中:

// using System.windows.input //使用System.windows.input

public Icommand EmptyCommand = ApplicationCommands.NotACommand;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM