相关疑难解决方法(0)

从视图模型(C#)设置焦点在WPF中的文本框

在我看来,我有一个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)

c# wpf xaml textbox mvvm

124
推荐指数
6
解决办法
11万
查看次数

标签 统计

c# ×1

mvvm ×1

textbox ×1

wpf ×1

xaml ×1