c# - IsMaskcompleted 有时为空,这是为什么呢?
问题描述
我正在开发一个新的 WPF 表单。我正在使用 Extended.Wpf.Toolkit 的 MaskedTextBox。我在 MultiBinding 表达式中使用 IsMaskCompleted 属性。这是 XAML:
<Border Grid.Row="1"
Grid.Column="1">
<Border.BorderThickness>
<MultiBinding Converter="{StaticResource multiBoolToThicknessConverter}">
<Binding ElementName="TargetMaskedTextBox, Path=IsMaskCompleted" />
<Binding Path="IsTargetCompleted" />
</MultiBinding>
</Border.BorderThickness>
<tk:MaskedTextBox x:Name="TargetMaskedTextBox"
Style="{StaticResource LeftAlignMaskedTextBoxStyle}"
Value="{Binding Target}"
IsEnabled="{Binding IsTargetEnabled}"
ValueDataType="{x:Type System:Single}"
Mask="\0.0##" />
</Border>
这是我的 IMultiValueConverter 中的 Convert 方法:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
try
{
bool? boolOneNullable = values[0] as bool?;
bool? boolTwoNullable = values[1] as bool?;
bool boolOne = true;
bool boolTwo = true;
if (!boolOneNullable.HasValue)
{
boolOne = false;
}
else
{
boolOne = boolOneNullable.Value;
}
if (!boolTwoNullable.HasValue)
{
boolTwo = false;
}
else
{
boolTwo = boolTwoNullable.Value;
}
var combinedBools = boolOne && boolTwo;
if (combinedBools)
{
return new Thickness(0);
}
else
{
return new Thickness(1);
}
}
catch (Exception)
{
return null;
}
}
我不明白的是,当在填充 MaskedTextBox 控件的数据网格中选择一行时,IsMaskCompleted 通常为空,即使其中有有效数据。这是为什么?
解决方案
您不能假设在您的Convert
方法第一次被调用时该属性已被初始化。稍后将再次调用它并使用实际值。
您应该检查是否提供了这些值,并且它们的简单返回不是:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 2 || (values[0] == null && values[1] == null))
return;
...
推荐阅读
- arrays - 为什么在尝试使用 transferArrayItem 将数组项从一个 Angular cdkDropList 移动到另一个时出现错误?
- java - JSON Schema 属性的多个实例
- python - Python:如何使用其他类产生的数据?
- google-apps-script - 用于编辑表单条目的 Google 触发器
- java - javafx形状的质心
- python - 在 Django REST Framework 中处理嵌套对象表示
- node.js - 如何解决 ELIFECYCLE npm Loopback 错误?
- javascript - 为什么在 JavaScript 中,MySql 时间戳在移动设备上的转换与在桌面设备上的转换不同?
- python-3.x - Python2 (str) 字节串
- html - 如何查看图像“onerror”事件的实际错误?