2
votes

let's say I have a several textboxes with with dependency to model that has IsDecimalAllowed property. So some of this textboxes have IsDecimalAllowed = true and some of them false.

So I need to determine which of this fields can take non-integer values and use this flag into TextBox_TextChanged event to remove extra characters or add input restriction.

Now I implemented it with Tag value of TextBox but it seems not the best desigion I could made..

XAML:

<TextBox  Text="{Binding DataValue, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleConverter}}" 
          InputScope="Number" 
          IsEnabled="{Binding IsEnabled}"
          TextChanged="TextBox_TextChanged"
          Tag="{Binding IsDecimalAllowed}">
          <!-- it would be nice to have custom property here -->
          <!-- for example IsDecimalAllowed="{Binding IsDecimalAllowed}" -->

TextBox_IsChanged:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;

        if (textBox == null)
            throw new InvalidCastException();

        bool? isAllowDecimalTag = textBox.Tag as bool?;

        if (isAllowDecimalTag == null)
            return;

        if (isAllowDecimalTag == false)
        {
            // some logic here
        }
        else if (isAllowDecimalTag == true)
        {
            // some logic here
        }
    }

I tried to find something and stumbled upon DependencyProperty. Is it able to implement it via DependencyObject or somehow?

Thank you in advance for any help.

2

2 Answers

3
votes

Steps you have to take (if had visual stu:

Extend TextBox:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App2
{
    public class CustomTextBox : TextBox
    {
        public CustomTextBox()
        {
            this.TextChanged += CustomTextBox_TextChanged;
        }



        public bool IsDecimalAllowed
        {
            get { return (bool)GetValue(IsDecimalAllowedProperty); }
            set { SetValue(IsDecimalAllowedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsDecimalAllowed.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsDecimalAllowedProperty =
            DependencyProperty.Register("IsDecimalAllowed", typeof(bool), typeof(CustomTextBox), new PropertyMetadata(true));



        private void CustomTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CustomTextBox textBox = sender as CustomTextBox;

            if (textBox == null)
                throw new InvalidCastException();

            bool? isAllowDecimalTag = textBox.IsDecimalAllowed;

            if (isAllowDecimalTag == null)
                return;

            if (isAllowDecimalTag == false)
            {
                // some logic here
            }
            else if (isAllowDecimalTag == true)
            {
                // some logic here
            }

        }
    }
}

And your xaml:

<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="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <local:CustomTextBox IsDecimalAllowed="True" />
    </Grid>
</Page>
1
votes

You are adding behavior to existing control, so you could extend it. Create new class, eg. MyTextBox, and inherit from TextBox. Add IsDecimalAllowed property and TextBox_TextChanged behavior there.