【发布时间】: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