none
TextBox有粘贴触发事件么?想在这个事件里对粘贴到文本框的内容进行验证或修改? RRS feed

  • 问题

  • 我做了一个只能输入数字的Textbox,使用PreviewTextInput事件实现,但是复制粘贴问题无法解决,使用TextChanged事件,如果涉及修改文本框内容,可能会导致死循环。

    我想禁止粘贴,或者粘贴之前验证,如何不合法,就不改变文本框内容,如何实现?

    2019年9月1日 10:11

答案

  • 我做了一个只能输入数字的Textbox,使用PreviewTextInput事件实现,但是复制粘贴问题无法解决,使用TextChanged事件,如果涉及修改文本框内容,可能会导致死循环。

    我想禁止粘贴,或者粘贴之前验证,如何不合法,就不改变文本框内容,如何实现?

    Hi   Trian555,   

    你可以通过DataObject.AddPastingHandler 对需要的元素添加事件,然后在事件中做验证或者取消。

       public textboxdiasblepaste()
            {
                InitializeComponent();
                DataObject.AddPastingHandler(textBox1, TextBoxPaste);
    
            }
    
    
            private void TextBoxPaste(object sender, DataObjectPastingEventArgs args)
            {
                string clipboard = args.DataObject.GetData(typeof(string)) as string;
    
    
                // you can check
                //MessageBox.Show(clipboard);
                
    
                args.CancelCommand();
            }
    
    

    或者在CommandManager.PreviewExecuted直接禁用Paste。

     <TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"
             ContextMenu="{x:Null}" Margin="0,0,0,359" />
    
      private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Command == ApplicationCommands.Copy ||
            e.Command == ApplicationCommands.Cut ||
            e.Command == ApplicationCommands.Paste)
                {
                   
    
                    e.Handled = true;
                }
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年9月16日 1:35
    2019年9月2日 4:00
    版主
  • Hi  Trian555,

    >> 这个能设置到textbox的style里的EventSetter里面吗?(事件代码存放在继承ResourceDictionary父类的partial修饰的类中)

    你可以尝试这样:

     <Window.Resources>
            <Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBox}">
                <EventSetter Event="CommandManager.PreviewExecuted" 
                     Handler="textBox_PreviewExecuted"/>
            </Style>
    
        </Window.Resources>
    
    
     <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="86" Margin="70,223,0,0" TextWrapping="Wrap" Text="TextBox111" VerticalAlignment="Top" Width="615" Style="{StaticResource ItemTextBlockEventSetterStyle}" />
    
    
           private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Command == ApplicationCommands.Copy ||
            e.Command == ApplicationCommands.Cut ||
            e.Command == ApplicationCommands.Paste)
                {
                   
    
                    e.Handled = true;
                }
            }

    或者

     <Style x:Key="ItemTextBlockEventSetterStyless" TargetType="{x:Type TextBox}">
                <EventSetter Event="DataObject.Pasting" 
                     Handler="TextBoxPaste"/>
            </Style>
    
      <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="86" Margin="70,223,0,0" TextWrapping="Wrap" Text="TextBox111" VerticalAlignment="Top" Width="615" Style="{StaticResource ItemTextBlockEventSetterStyless}" />
    
      private void TextBoxPaste(object sender, DataObjectPastingEventArgs args)
            {
                string clipboard = args.DataObject.GetData(typeof(string)) as string;
    
                // you can check
                //MessageBox.Show(clipboard);
                
                args.CancelCommand();
            }
    
    
    
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年9月16日 1:34
    2019年9月3日 2:35
    版主

全部回复

  • 我做了一个只能输入数字的Textbox,使用PreviewTextInput事件实现,但是复制粘贴问题无法解决,使用TextChanged事件,如果涉及修改文本框内容,可能会导致死循环。

    我想禁止粘贴,或者粘贴之前验证,如何不合法,就不改变文本框内容,如何实现?

    Hi   Trian555,   

    你可以通过DataObject.AddPastingHandler 对需要的元素添加事件,然后在事件中做验证或者取消。

       public textboxdiasblepaste()
            {
                InitializeComponent();
                DataObject.AddPastingHandler(textBox1, TextBoxPaste);
    
            }
    
    
            private void TextBoxPaste(object sender, DataObjectPastingEventArgs args)
            {
                string clipboard = args.DataObject.GetData(typeof(string)) as string;
    
    
                // you can check
                //MessageBox.Show(clipboard);
                
    
                args.CancelCommand();
            }
    
    

    或者在CommandManager.PreviewExecuted直接禁用Paste。

     <TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"
             ContextMenu="{x:Null}" Margin="0,0,0,359" />
    
      private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Command == ApplicationCommands.Copy ||
            e.Command == ApplicationCommands.Cut ||
            e.Command == ApplicationCommands.Paste)
                {
                   
    
                    e.Handled = true;
                }
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年9月16日 1:35
    2019年9月2日 4:00
    版主
  • 我做了一个只能输入数字的Textbox,使用PreviewTextInput事件实现,但是复制粘贴问题无法解决,使用TextChanged事件,如果涉及修改文本框内容,可能会导致死循环。

    我想禁止粘贴,或者粘贴之前验证,如何不合法,就不改变文本框内容,如何实现?

    Hi   Trian555,   

    你可以通过DataObject.AddPastingHandler 对需要的元素添加事件,然后在事件中做验证或者取消。

       public textboxdiasblepaste()
            {
                InitializeComponent();
                DataObject.AddPastingHandler(textBox1, TextBoxPaste);
    
            }
    
    
            private void TextBoxPaste(object sender, DataObjectPastingEventArgs args)
            {
                string clipboard = args.DataObject.GetData(typeof(string)) as string;
    
    
                // you can check
                //MessageBox.Show(clipboard);
                
    
                args.CancelCommand();
            }
    

    或者在CommandManager.PreviewExecuted直接禁用Paste。

     <TextBox CommandManager.PreviewExecuted="textBox_PreviewExecuted"
             ContextMenu="{x:Null}" Margin="0,0,0,359" />
    
      private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Command == ApplicationCommands.Copy ||
            e.Command == ApplicationCommands.Cut ||
            e.Command == ApplicationCommands.Paste)
                {
                   
    
                    e.Handled = true;
                }
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    这个能设置到textbox的style里的EventSetter里面吗?(事件代码存放在继承ResourceDictionary父类的partial修饰的类中)
    2019年9月2日 5:26
  • Hi  Trian555,

    >> 这个能设置到textbox的style里的EventSetter里面吗?(事件代码存放在继承ResourceDictionary父类的partial修饰的类中)

    你可以尝试这样:

     <Window.Resources>
            <Style x:Key="ItemTextBlockEventSetterStyle" TargetType="{x:Type TextBox}">
                <EventSetter Event="CommandManager.PreviewExecuted" 
                     Handler="textBox_PreviewExecuted"/>
            </Style>
    
        </Window.Resources>
    
    
     <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="86" Margin="70,223,0,0" TextWrapping="Wrap" Text="TextBox111" VerticalAlignment="Top" Width="615" Style="{StaticResource ItemTextBlockEventSetterStyle}" />
    
    
           private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                if (e.Command == ApplicationCommands.Copy ||
            e.Command == ApplicationCommands.Cut ||
            e.Command == ApplicationCommands.Paste)
                {
                   
    
                    e.Handled = true;
                }
            }

    或者

     <Style x:Key="ItemTextBlockEventSetterStyless" TargetType="{x:Type TextBox}">
                <EventSetter Event="DataObject.Pasting" 
                     Handler="TextBoxPaste"/>
            </Style>
    
      <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="86" Margin="70,223,0,0" TextWrapping="Wrap" Text="TextBox111" VerticalAlignment="Top" Width="615" Style="{StaticResource ItemTextBlockEventSetterStyless}" />
    
      private void TextBoxPaste(object sender, DataObjectPastingEventArgs args)
            {
                string clipboard = args.DataObject.GetData(typeof(string)) as string;
    
                // you can check
                //MessageBox.Show(clipboard);
                
                args.CancelCommand();
            }
    
    
    
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Trian555 2019年9月16日 1:34
    2019年9月3日 2:35
    版主