![](/img/trans.png)
[英]How do I check if two Objects are equal in terms of their properties only without breaking the existing Object.Equals()?
[英]How do I disable dash + equals on a check box object in xaml?
我有一個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>
我想知道,我如何禁用使用' - '和'='作為chek並取消選中的默認復選框行為。
我想允許該文本框中的用戶為頁面范圍鍵入“1-5,6,7”,但不能這樣做,因為' - '正在檢查並取消選中該框
編輯:我想保持空格鍵功能檢查+取消選中復選框。 無論出於何種原因,如果我在文本框中並輸入空格,它不會觸發切換事件,但是' - '和'='會觸發。
EDIT2:理想情況下尋找xaml修復程序,因為我正在嘗試維護MVVM體系結構,並且不希望背后有代碼
CheckBox.OnKeyDown是罪魁禍首(請參閱http://msdn.microsoft.com/en-us/library/system.windows.controls.checkbox.onkeydown.aspx )。 我通過創建一個覆蓋OnKeyDown並且什么都不做的自定義CheckBox類來解決這個問題。 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
只要您需要復選框忽略鍵盤輸入,就可以使用此類而不是CheckBox。
從這里可以看出,C#.NET中的CheckBox.cs源代碼 ,對於2狀態復選框, Key.Add, Key.Subtract, Key.OemPlus, Key.OemMinus
有自定義行為。
(以編程方式)添加命令對我不起作用。 覆蓋子類時:
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;
}
}
嘗試使用空命令覆蓋鍵盤快捷鍵:
<RadioButton>
<KeyBinding Key="OemMinus" Command="{Binding EmptyCommand}"/>
</RadioButton>
並在您的viewmodel中:
//使用System.windows.input
public Icommand EmptyCommand = ApplicationCommands.NotACommand;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.