将自定义属性绑定到文本框UWP



假设我有一个带有依赖性模型的文本框,这些文本框具有iSdecimalallowed属性。因此,其中一些文本框具有IsDecimalAllowed = true,其中一些false

因此,我需要确定哪个字段可以采用非直集值,然后将此标志使用到textbox_textchanged事件中以删除额外的字符或添加输入限制。

现在我用文本框的标签值实现了它,但似乎不是我能做到的最好的desigion。

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

我试图找到一些东西,偶然发现了依赖性。它可以通过依赖项或某种方式实现它吗?

预先感谢您的任何帮助。

您必须采取的步骤(如果有Visual Stu:

扩展文本框:

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>

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

相关内容

  • 没有找到相关文章

最新更新