将焦点设置文本框在WPF从视图模型(C#) [英] Set focus on textbox in WPF from view model (C#)
问题描述
我有一个文本框
和按钮
在我看来。
现在我检查后,按一下按钮一个条件,如果条件证明是假的,显示消息给用户,然后我必须设置光标到文本框控件。
Now I am checking a condition upon button click and if the condition turns out to be false, displaying the message to the user, and then I have to set the cursor to the text box control.
if (companyref == null)
{
Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation cs = new Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation();
MessageBox.Show("Company does not exist.", "Error", MessageBoxButton.OK,
MessageBoxImage.Exclamation);
cs.txtCompanyID.Focusable = true;
System.Windows.Input.Keyboard.Focus(cs.txtCompanyID);
}
以上code是在视图模型。
The above code is in the view model.
该CompanyAssociation认为名字。
The CompanyAssociation is the view name.
但光标未在文本框设置越来越
。
在XAML是因为在
<igEditors:XamTextEditor KeyDown="xamTextEditorAllowOnlyNumeric_KeyDown"
Name="txtCompanyID" ValueChanged="txtCompanyID_ValueChanged" Text="{Binding
Company.CompanyId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding
ActualWidth, ElementName=border}" Grid.Column="1" Grid.Row="0"
VerticalAlignment="Top" Margin="0,5,0,0" HorizontalAlignment="Stretch"
IsEnabled="{Binding Path=IsEditable}" />
<Button Template="{StaticResource buttonTemp1}" Command="{Binding ContactCommand}"
CommandParameter="searchCompany" Content="Search" Width="80" Grid.Column="2"
Grid.Row="0" VerticalAlignment="Top" Margin="0" HorizontalAlignment="Left"
IsEnabled="{Binding Path=IsEditable}" />
请帮忙
推荐答案
让我来回答你的问题三个部分。
Let me answer to your question in three parts.
-
我不知道什么是cs.txtCompanyID在你的榜样?它是一个TextBox控件?如果是,那么你是在一个错误的方式。一般来讲这不是一个好主意,有任何引用您的视图模型来UI。你可以问为什么?不过这是另外一个问题后#2。)
I'm wondering what is "cs.txtCompanyID" in your example? Is it a TextBox control? If yes, then you are on a wrong way. Generally speaking it's not a good idea to have any reference to UI in your ViewModel. You can ask "Why?" but this is another question to post on Stackoverflow :).
跟踪与焦点问题的最好办法是...调试.NET源$ C $ C。别开玩笑了。它救了我很多时间很多次。为了让.NET源$ C $ C调试参考<一个href=\"http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-$c$c.aspx\">Shawn Bruke的博客。
The best way to track down issues with Focus is... debugging .Net source code. No kidding. It saved me a lot of time many times. To enable .net source code debugging refer to Shawn Bruke's blog.
最后,我用它来设置焦点从视图模型的普遍做法是附加属性。我写了很简单的附加属性,它可以在任何的UIElement设置。它可以被绑定到视图模型的财产IsFocused为例。在这里,它是:
Finally, general approach that I use to set focus from ViewModel is Attached Properties. I wrote very simple attached property, which can be set on any UIElement. And it can be bound to ViewModel's property "IsFocused" for example. Here it is:
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool) obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof (bool), typeof (FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement) d;
if ((bool) e.NewValue)
{
uie.Focus(); // Don't care about false values.
}
}
}
现在在你的视图(XAML)可以将此属性绑定到您的视图模型:
Now in your View (in XAML) you can bind this property to your ViewModel:
<TextBox local:FocusExtension.IsFocused="{Binding IsUserNameFocused}" />
希望这有助于:)。如果它不参考答案#2。
Hope this helps :). If it doesn't refer to the answer #2.
干杯。
这篇关于将焦点设置文本框在WPF从视图模型(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!