将自定义属性绑定到TextBox UWP

时间:2018-03-25 11:54:52

标签: c# xaml data-binding uwp dependency-properties

让我们说我有几个文本框,依赖于具有IsDecimalAllowed属性的模型。 因此,其中一些文本框包含IsDecimalAllowed = true,其中一些false

所以我需要确定哪个字段可以采用非整数值,并将此标志用于TextBox_TextChanged事件以删除多余字符或添加输入限制。

现在我使用TextBox的Tag值实现它,但它似乎不是我能做的最好的设计..

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
        }
    }

我试图找到一些东西并偶然发现DependencyProperty。它是否能够通过DependencyObject或某种方式实现它?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

你必须采取的步骤(如果有视觉效果:

扩展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
            }

        }
    }
}

你的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 :(得分:1)

您正在向现有控件添加行为,因此您可以对其进行扩展。创建新类,例如。 MyTextBox,并继承自TextBox。在那里添加IsDecimalAllowed属性和TextBox_TextChanged行为。