locked
set property threw an exception RRS feed

  • Question

  • hi,

    I use watermark like this in silverlight 5, why it throws an exception?

     <Grid x:Name="LayoutRoot" Background="White">

            <TextBox Watermark="This is my watermark" Margin="20"></TextBox>

        </Grid>

    how can I use this kind of property,do i miss something?

    Tuesday, May 6, 2014 2:08 PM

Answers

  • Hi,

    Please try to create one Class library project. Then add Class File using the Following code. After that please add this dll in your project.

    public class WatermarkTextBox : TextBox 
    { 
        private bool displayWatermark = true; 
        private bool hasFocus = false; 
         public WatermarkTextBox() 
        { 
            this.GotFocus += new RoutedEventHandler(WatermarkTextBox_GotFocus); 
            this.LostFocus += new RoutedEventHandler(WatermarkTextBox_LostFocus); 
            this.TextChanged += new TextChangedEventHandler(WatermarkTextBox_TextChanged); 
            this.Unloaded += new RoutedEventHandler(WatermarkTextBox_Unloaded); 
        } 
    
        private void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e) 
        { 
            if (!hasFocus && Text == "") 
            { 
                setMode(true); 
                displayWatermark = true; 
                this.Text = Watermark; 
            } 
        } 
    
        private void WatermarkTextBox_Unloaded(object sender, RoutedEventArgs e) 
        { 
            this.GotFocus -= WatermarkTextBox_GotFocus; 
            this.LostFocus -= WatermarkTextBox_LostFocus; 
            this.Unloaded -= WatermarkTextBox_Unloaded; 
            this.TextChanged -= WatermarkTextBox_TextChanged; 
        } 
    
        private void WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e) 
        { 
            hasFocus = true; 
            if (displayWatermark) 
            { 
                setMode(false); 
                this.Text = ""; 
            } 
        } 
        private void WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e) 
        { 
            hasFocus = false; 
            if (this.Text == "") 
            { 
                displayWatermark = true; 
                setMode(true); 
                this.Text = Watermark; 
            } 
            else 
            { 
                displayWatermark = false; 
            } 
        } 
        private void setMode(bool watermarkStyle) 
        { 
            if (watermarkStyle) 
            { 
                this.FontStyle = FontStyles.Italic; 
                this.Foreground = new SolidColorBrush(Colors.Gray); 
            } 
            else 
            { 
                this.FontStyle = FontStyles.Normal; 
                this.Foreground = new SolidColorBrush(Colors.Black); 
            } 
        } 
        public new string Watermark 
        { 
            get { return GetValue(WatermarkProperty) as string; } 
            set { SetValue(WatermarkProperty, value); } 
        } 
        public static new readonly DependencyProperty WatermarkProperty = 
            DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(watermarkPropertyChanged)); 
        private static void watermarkPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
        { 
            WatermarkTextBox textBox = obj as WatermarkTextBox; 
            if (textBox.displayWatermark) 
            { 
                textBox.Text = e.NewValue.ToString(); 
                textBox.setMode(true); 
            } 
        } 

    In the MainPage.xaml:

    xmlns:watertext="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
    
        <watertext:WatermarkTextBox Watermark="This is my watermark" Margin="150,150,150,150"></watertext:WatermarkTextBox>
    
    
     


    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, May 7, 2014 5:08 AM
    Moderator

All replies

  • Hi.

    Are you sure TextBox class has Watermark property?

    This property is not implemented.

    Take a look at this;

    Hope this helps you.

    Tuesday, May 6, 2014 3:33 PM
  • Hi,

    Please try to create one Class library project. Then add Class File using the Following code. After that please add this dll in your project.

    public class WatermarkTextBox : TextBox 
    { 
        private bool displayWatermark = true; 
        private bool hasFocus = false; 
         public WatermarkTextBox() 
        { 
            this.GotFocus += new RoutedEventHandler(WatermarkTextBox_GotFocus); 
            this.LostFocus += new RoutedEventHandler(WatermarkTextBox_LostFocus); 
            this.TextChanged += new TextChangedEventHandler(WatermarkTextBox_TextChanged); 
            this.Unloaded += new RoutedEventHandler(WatermarkTextBox_Unloaded); 
        } 
    
        private void WatermarkTextBox_TextChanged(object sender, TextChangedEventArgs e) 
        { 
            if (!hasFocus && Text == "") 
            { 
                setMode(true); 
                displayWatermark = true; 
                this.Text = Watermark; 
            } 
        } 
    
        private void WatermarkTextBox_Unloaded(object sender, RoutedEventArgs e) 
        { 
            this.GotFocus -= WatermarkTextBox_GotFocus; 
            this.LostFocus -= WatermarkTextBox_LostFocus; 
            this.Unloaded -= WatermarkTextBox_Unloaded; 
            this.TextChanged -= WatermarkTextBox_TextChanged; 
        } 
    
        private void WatermarkTextBox_GotFocus(object sender, RoutedEventArgs e) 
        { 
            hasFocus = true; 
            if (displayWatermark) 
            { 
                setMode(false); 
                this.Text = ""; 
            } 
        } 
        private void WatermarkTextBox_LostFocus(object sender, RoutedEventArgs e) 
        { 
            hasFocus = false; 
            if (this.Text == "") 
            { 
                displayWatermark = true; 
                setMode(true); 
                this.Text = Watermark; 
            } 
            else 
            { 
                displayWatermark = false; 
            } 
        } 
        private void setMode(bool watermarkStyle) 
        { 
            if (watermarkStyle) 
            { 
                this.FontStyle = FontStyles.Italic; 
                this.Foreground = new SolidColorBrush(Colors.Gray); 
            } 
            else 
            { 
                this.FontStyle = FontStyles.Normal; 
                this.Foreground = new SolidColorBrush(Colors.Black); 
            } 
        } 
        public new string Watermark 
        { 
            get { return GetValue(WatermarkProperty) as string; } 
            set { SetValue(WatermarkProperty, value); } 
        } 
        public static new readonly DependencyProperty WatermarkProperty = 
            DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox), new PropertyMetadata(watermarkPropertyChanged)); 
        private static void watermarkPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
        { 
            WatermarkTextBox textBox = obj as WatermarkTextBox; 
            if (textBox.displayWatermark) 
            { 
                textBox.Text = e.NewValue.ToString(); 
                textBox.setMode(true); 
            } 
        } 

    In the MainPage.xaml:

    xmlns:watertext="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
    
        <watertext:WatermarkTextBox Watermark="This is my watermark" Margin="150,150,150,150"></watertext:WatermarkTextBox>
    
    
     


    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, May 7, 2014 5:08 AM
    Moderator