WPF MVVM Style中使用事件

View的Style中设置事件

<Style TargetType="TextBox">
            <EventSetter Event="GotFocus" Handler="TextBox_GotFocus" />
            <EventSetter Event="LostFocus" Handler="TextBox_LostFocus" />
</Style>

在View.xmal.cs文件中定义事件的处理方法与ViewModel进行交互

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox.Name == "TerminalNo")
            {
                if (this.DataContext != null && this.DataContext is SettingViewModel settingViewModel)
                {
                    settingViewModel.ConfirmCommand.Execute(null);
                }
            }
        }

个人认为MVVM并不是就禁止在View.xmal.cs中编写代码,MVVM主要做的还是业务与UI分离.

猜你喜欢

转载自www.cnblogs.com/doubanjiang/p/9686226.html
今日推荐