Wpf code behind call viewModel method

public MessageView()
        {
            InitializeComponent();
            this.KeyDown += MessageView_KeyDown;
        }
        
        private void MessageView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var vm = this.DataContext as MessageViewModel;
                if(vm!=null)
                {
                    vm.SendMessage();
                    ShowTextBoxContent(vm.TbText);
                }
            }
        }

        StringBuilder textBuilder = new StringBuilder();
        private void ShowTextBoxContent(string msg)
        {
            textBuilder.AppendLine(msg);
            tbx.Text = textBuilder.ToString();
        }

 

posted @ 2021-03-23 11:33  FredGrit  阅读(49)  评论(0编辑  收藏  举报