【问题标题】:UserControl doesn't show validation exception in SilverlightUserControl 在 Silverlight 中不显示验证异常
【发布时间】:2011-09-15 09:28:41
【问题描述】:

我有一个用户控件,我已经将它的依赖属性 TextValue 绑定到一个视图模型 RightSpecGlassStrength

用户控制代码

<UserControl x:Class="NumericUpDown1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="25" d:DesignWidth="70">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
        <TextBox x:Name="InputTextBox" Grid.Row="0" Grid.Column="0" Grid.RowSpan="1"
                 Style="{StaticResource NumericUpDownTextBoxStyle}"
                 KeyDown="InputTextBox_KeyDown" 
                 KeyUp="InputTextBox_KeyUp"
                 GotFocus="InputTextBox_GotFocus" 
                 LostFocus="InputTextBox_LostFocus"  
                 MouseWheel="InputTextBox_MouseWheel"
                 MouseEnter="InputTextBox_MouseEnter"
                 TextInputStart="InputTextBox_TextInputStart"
                 LayoutUpdated="InputTextBox_LayoutUpdated"
                 />
    </StackPanel>

</UserControl>

ViewItems.Xaml

  <userControl:NumericUpDown1 x:Name="RightSpecGlassStrengthUpDown" Maximum="28" Minimum="-28" Step="0.25"  TextValue="{Binding RightSpecGlassStrength, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" TabIndex="5"   />

ViewItemsViewModel.cs

  public class ViewItemsViewModel : EntityViewModel
{

    #region constructor
    public ViewItemsViewModel(){}
    #endregion

    #region properties


    private Double rightSpecGlassStrength;
    public Double RightSpecGlassStrength
    {
        get
        {
            return rightSpecGlassStrength;
        }
        set
        {
            rightSpecGlassStrength=value;
            ValidateStrengths("RightSpecGlassStrength", RightSpecGlassStrength);
            PropertyChangedHandler("RightSpecGlassStrength");
        }
    }

    private void ValidateStrengths(string propertyName1, double RightSpecGlassStrength)
    {
        ClearErrorFromProperty(propertyName1);
        if (RightSpecGlassStrength == 0)
            AddErrorForProperty(propertyName1, "Value can not be 0");
    }


    #endregion
}

我的 EntityViewModel.cs 正在实现 INotifyDataErrorInfo 接口并继承 ViewModelBase 类

 public class EntityViewModel : ViewModelBase, INotifyDataErrorInfo
    {
      }

ViewModelBase.cs 实现 INotifyPropertyChanged

 public class ViewModelBase : INotifyPropertyChanged
    {
}

当我将文本框或其他 silverlight 控件绑定到视图模型时,我的代码可以正常工作。 并在控件上显示正确的验证异常。

但是当用户控件获得验证异常时,控件不会显示任何异常。

我没有发现用户控制有什么问题。???

【问题讨论】:

  • “当用户控件获得验证异常时”是什么意思?

标签: wpf silverlight user-controls


【解决方案1】:

如果没有用户控件背后的代码,很难完全调试问题,但是,在我看来,您对背后的代码有一个依赖属性,如下所示:

public static DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(NumericUpDown1), null)

然后您似乎正在使用 TextInputStart、KeyDown、KeyUp 事件来捕获对基础控件的更改。我的预感是: a) 您未能将 TextValue 属性更新为新值。 b) 您背后的代码干扰了验证过程。

我建议您在 xaml 中命名用户控件,而不是隐藏代码;即

<UserControl x:Class="NumericUpDown1" x:Name="View"> ... </UserControl>

然后将底层TextBox的文本值直接绑定到依赖属性上,像这样:

<TextBox Text="{Binding ElementName=View, Path=TextValue, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ... />

这应该允许验证机制正常进行。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多