locked
In WinRT, How to find which command executed from ContextMenu in TextBox RRS feed

  • Question

  • Hi,

    I have created a NumericTextBox inherited from TextBox in WinRT. I need to validate the Text while Cut, Paste. I am not able to find how to validate the Text when Cut,Paste is done through ContextMenu. In WPF, I have used previewExecuted event from CommandManager class which is not available in WinRT. Could anyone help on this.

    Regards,

    Swasthika

    Monday, September 3, 2012 6:22 AM

Answers

  • The following code answers "I need to validate the Text while Cut, Paste.":

    <Page
        x:Class="App2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBox Text="Text1"/>
                <!-- This is just an example of feedback to the user in case of error [paste or data entry]-->
                <Border x:Name="b2" BorderThickness="1" BorderBrush="Transparent">
                    <TextBox Text="{Binding Path=Id, Mode=TwoWay}" InputScope="Number" TextChanged="TextChanged"/>
                </Border>
            </StackPanel>
        </Grid>
    </Page>

    -

    using Windows.UI;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    
    namespace App2
    {
        /// <summary>
        ///
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = new KiwiItem();
            }
            private void TextChanged(object sender, TextChangedEventArgs e)
            {
                double num;
                b2.BorderBrush = 
                    string.IsNullOrEmpty(((TextBox)sender).Text.Trim()) || double.TryParse(((TextBox)sender).Text.Trim(), out num) ?
                    new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Red);
            }
        }
    }
    

    -

    using System.ComponentModel;
    using System.Diagnostics;
    
    namespace App2
    {
        public class KiwiItem : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            double id = 2.34;
            public double Id
            {
                get { return id; }
                set
                {
                    id = value;
                    OnPropertyChanged("Id");
                    Debug.WriteLine("Id:{0}", value);
                }
            }
        }
    }
    

    • Marked as answer by Aaron Xue Wednesday, September 12, 2012 6:47 AM
    Monday, September 3, 2012 10:45 AM
  • I see. The following is a possible workaround, most unfortunately at the expense of designing your own popup:

    http://code.msdn.microsoft.com/windowsapps/Context-menu-sample-40840351

    Refer in particular to Scenario 2.

    • Marked as answer by Aaron Xue Wednesday, September 12, 2012 6:47 AM
    Friday, September 7, 2012 9:09 AM

All replies

  • The following code answers "I need to validate the Text while Cut, Paste.":

    <Page
        x:Class="App2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBox Text="Text1"/>
                <!-- This is just an example of feedback to the user in case of error [paste or data entry]-->
                <Border x:Name="b2" BorderThickness="1" BorderBrush="Transparent">
                    <TextBox Text="{Binding Path=Id, Mode=TwoWay}" InputScope="Number" TextChanged="TextChanged"/>
                </Border>
            </StackPanel>
        </Grid>
    </Page>

    -

    using Windows.UI;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Media;
    
    namespace App2
    {
        /// <summary>
        ///
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.DataContext = new KiwiItem();
            }
            private void TextChanged(object sender, TextChangedEventArgs e)
            {
                double num;
                b2.BorderBrush = 
                    string.IsNullOrEmpty(((TextBox)sender).Text.Trim()) || double.TryParse(((TextBox)sender).Text.Trim(), out num) ?
                    new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Red);
            }
        }
    }
    

    -

    using System.ComponentModel;
    using System.Diagnostics;
    
    namespace App2
    {
        public class KiwiItem : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            double id = 2.34;
            public double Id
            {
                get { return id; }
                set
                {
                    id = value;
                    OnPropertyChanged("Id");
                    Debug.WriteLine("Id:{0}", value);
                }
            }
        }
    }
    

    • Marked as answer by Aaron Xue Wednesday, September 12, 2012 6:47 AM
    Monday, September 3, 2012 10:45 AM
  • Hi,

    Thanks for the update. I have already done this validation using TextChanged. While pasting through ContextMenu, I need to perform different kind of validation than the normal validation. That's why I want to identify when Text was pasted using ContextMenu. Could anyone help me on this?

    Regards,

    Swasthika


    Swasthika

    Wednesday, September 5, 2012 8:49 AM
  • I see. The following is a possible workaround, most unfortunately at the expense of designing your own popup:

    http://code.msdn.microsoft.com/windowsapps/Context-menu-sample-40840351

    Refer in particular to Scenario 2.

    • Marked as answer by Aaron Xue Wednesday, September 12, 2012 6:47 AM
    Friday, September 7, 2012 9:09 AM