wpf 如何在按钮按下时失去注意力?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12055332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to lose focus on button press?
提问by Sam
Simplified: In my view I've got a xaml page containing a button and some kind of TextBox. The button is bound to a DelegateCommand in the ViewModel, the editbox is bound to some property in the ViewModel.
简化:在我看来,我有一个 xaml 页面,其中包含一个按钮和某种文本框。按钮绑定到 ViewModel 中的 DelegateCommand,编辑框绑定到 ViewModel 中的某些属性。
View:
<Button Command="{Binding MyCommand}"/>
<TextBox Text="{Binding MyString}"/>
ViewModel:
public DelegateCommand MyCommand { get; set; }
public String MyString { get; set; }
Now when the user enters something into the box and clicks on the button, the button does not receive a focus change event. So it will not update it's content to the property. So the property MyString does not reflect the content of the TextBox on clicking the button. So whatever processing MyCommand is doing, it is working with old data and not the current input.
现在,当用户在框中输入内容并单击按钮时,按钮不会收到焦点更改事件。所以它不会更新它的内容到属性。因此属性 MyString 不反映单击按钮时 TextBox 的内容。因此,无论 MyCommand 正在做什么处理,它都在处理旧数据而不是当前输入。
Now if this really was just a TextBox I'd add UpdateSourceTrigger=PropertyChanged to the binding and I'd be good. But in this case the edit control is a bit more complex and needs to do some validation to the content. So I need some kind of "lost focus" signal when pressing the button by mouse.
现在,如果这真的只是一个 TextBox,我会将 UpdateSourceTrigger=PropertyChanged 添加到绑定中,我会很好。但在这种情况下,编辑控件有点复杂,需要对内容进行一些验证。所以当用鼠标按下按钮时,我需要某种“失去焦点”的信号。
My problem is: in MVVM the code behind for the button does not have any access to the view, so it can't make it lose the focus.
我的问题是:在 MVVM 中,按钮背后的代码无法访问视图,因此无法使其失去焦点。
Is there any way in xaml (e.g. in the view) to make the button get the keyboard focus when it is clicked by mouse? That would be the easiest way for my custom control to get a "lost focus" message.
xaml(例如在视图中)有什么方法可以使按钮在鼠标单击时获得键盘焦点?这将是我的自定义控件获得“失去焦点”消息的最简单方法。
采纳答案by SpeedCoder5
Re: Is there any way in xaml (e.g. in the view) to make the button get the keyboard focus when it is clicked by mouse? > A button does receive keyboard focus when it is clicked – provided the IsEnabled, Focusable, and IsHitTestVisible properties are all set to true as they are by default. To set keyboard focus programmatically call Keybaord.Focus as shown in the example below.
回复:xaml(例如在视图中)有什么方法可以使按钮在鼠标单击时获得键盘焦点?> 按钮在被点击时会获得键盘焦点——前提是 IsEnabled、Focusable 和 IsHitTestVisible 属性都设置为默认情况下的 true。要以编程方式设置键盘焦点,请调用 Keybaord.Focus,如下例所示。
Contrary to a popular meme, Commands do not HAVE to be processed in the VM. If, on command execution, the view needs to be changed independently of the VM, the command can be handled in the view. In fact this is the native pattern for WPF.
与流行的模因相反,命令不必在 VM 中处理。如果在命令执行时需要独立于 VM 更改视图,则可以在视图中处理命令。事实上,这是 WPF 的原生模式。
The following example shows using the Command property on a button to process a command in the view. For simplicity the example does not have a VM. Even though the command is processed in the view, if there were a VM, the code behind in the view could make calls into it.
以下示例显示如何使用按钮上的 Command 属性来处理视图中的命令。为简单起见,该示例没有 VM。即使命令是在视图中处理的,如果有虚拟机,视图背后的代码也可以调用它。
public partial class MainWindow : Window
{
public static readonly RoutedUICommand MyCommand = new RoutedUICommand("My Command", "MyCommand", typeof(MainWindow));
public String MyString { get; set; }
public MainWindow()
{
MyString = "some text";
DataContext = this; // this simple example has no VM
InitializeComponent();
}
private void MyCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Button1.Content = MyString;
Keyboard.Focus(Button1);
}
}
<Window x:Class="fwLoseFocus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:fwLoseFocus">
<Window.CommandBindings>
<CommandBinding Command="me:MainWindow.MyCommand" Executed="MyCommand_Executed"/>
</Window.CommandBindings>
<StackPanel>
<TextBox Text="{Binding MyString}"/>
<Button x:Name="Button1" Command="me:MainWindow.MyCommand"/>
</StackPanel>
</Window>
回答by Manoj
Is it not possible to still have the click event for the Button and have the code behind make the textbox lose focus?
是否不可能仍然有按钮的点击事件并且背后的代码使文本框失去焦点?
private void Button_Click(object sender, RoutedEventArgs e) {
FocusManager.SetFocusedElement(this, null);
}
Is the following answer relavant to you?
以下答案与您有关吗?

