【问题标题】:Binding custom property to TextBox UWP将自定义属性绑定到 TextBox UWP
【发布时间】:2018-09-03 15:48:02
【问题描述】:

假设我有几个文本框依赖于具有 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 或以某种方式实现它?

提前感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      扩展文本框:

      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>
      

      【讨论】:

      • 谢谢。这正是我所需要的。你节省了我的时间。
      猜你喜欢
      • 2021-11-27
      • 2017-11-08
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      相关资源
      最近更新 更多