在我看来,我有一个TextBox和一个Button.
现在我在点击按钮时检查条件,如果条件结果为假,向用户显示消息,然后我必须将光标设置为TextBox控件.
if (companyref == null)
{
var 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);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在ViewModel中.
这CompanyAssociation是视图名称.
但光标没有设置在TextBox.
xaml是:
<igEditors:XamTextEditor Name="txtCompanyID"
KeyDown="xamTextEditorAllowOnlyNumeric_KeyDown"
ValueChanged="txtCompanyID_ValueChanged"
Text="{Binding Company.CompanyId,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Width="{Binding ActualWidth, ElementName=border}"
Grid.Column="1" Grid.Row="0"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
Margin="0,5,0,0"
IsEnabled="{Binding Path=IsEditable}"/>
<Button Template="{StaticResource buttonTemp1}"
Command="{Binding ContactCommand}"
CommandParameter="searchCompany"
Content="Search"
Width="80"
Grid.Row="0" Grid.Column="2"
VerticalAlignment="Top"
Margin="0"
HorizontalAlignment="Left"
IsEnabled="{Binding Path=IsEditable}"/>
Run Code Online (Sandbox Code Playgroud) 如何从ViewModel wpf中聚焦文本框?
<TextBox Name="PropertySearch"
Text="{Binding UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay, Path=PropertySearch,
ValidatesOnDataErrors=True}"
Width="110"
Height="25"
Margin="10" />
Run Code Online (Sandbox Code Playgroud) 我创建了UserControl一个在WPF中的视图(窗口)中加载的.在我的用户控件中,我放了一个TextBox.当我的视图加载时,我无法将焦点设置在此文本框上.我试过以下但没有什么对我有用:
FocusManager.FocusedElement="{Binding ElementName=PwdBox}"
我创建了一个FocusExtension专注于控制的东西.
请帮忙.
我有一个表单,我想在某些用户操作发生时将焦点设置为文本框.我知道MVVM的做法是绑定到VM属性,但是TextBox没有允许这种情况发生的属性.从VM设置焦点的最佳方法是什么?