UserControl不会在Silverlight中显示验证异常

时间:2011-09-15 09:28:41

标签: wpf silverlight user-controls

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

UserControl代码

<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控件绑定到viewmodel时,我的代码工作正常。 并在控件上显示正确的验证异常。

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

我没有弄错用户控制。???

1 个答案:

答案 0 :(得分:0)

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

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}" ... />

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

希望它有所帮助。