C# Button获取TextBox内容时,当TextBox内容改变,按Enter键,属性更改通知传不到button事件中

<Window x:Class="按钮测试enter键.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:按钮测试enter键"
         mc:Ignorable="d"
         Title="MainWindow" Height="350" Width="525">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="*"/>
             <RowDefinition Height="*"/>
         </Grid.RowDefinitions>
         <TextBox Name="txt" VerticalAlignment="Bottom" IsEnabled="True" Grid.Row="0" Height="20" Width="200" Text="{Binding FileName}"/>
         <Button Grid.Row="1" VerticalAlignment="Top" Margin="20" IsDefault="True" Content="确认" Height="30" Width="40" Click="Button_Click"/>
     </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace 按钮测试enter键
{
     /// <summary>
     /// MainWindow.xaml 的交互逻辑
     /// </summary>
     public partial class MainWindow : Window
     {
         VM vm = new VM();
         public MainWindow()
         {
             InitializeComponent();
             this.DataContext = vm;
         }
 
         private void Button_Click(object sender, RoutedEventArgs e)
         {
             //MessageBox.Show(txt.Text);
             MessageBox.Show(vm.FileName);
         }
     }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace 按钮测试enter键
{
     class VM : PropertyChangedBase
     {
 
         string fileName;
         public string FileName
         {
             get { return fileName; }
             set
             {
                 if (fileName != value)
                 {
                     int index = -1;
                     if ((index = value.IndexOf(".")) > 0)
                     {
                         value = value.Substring(0, index);r
                     }
                     fileName = value;
                     OnPropertyChanged("FileName");
                 }
             }
         }
     }
 
     class PropertyChangedBase : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;
 
         protected virtual void OnPropertyChanged(string propertyName)
         {
             if (this.PropertyChanged != null)
             {
                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
             }
         }
     }
}

按Enter后

 

 

posted @ 2022-07-01 17:04  羽小兮  阅读(261)  评论(0)    收藏  举报