开发者

UserControl doesn't show validation exception in Silverlight

I have a user control and I have开发者_JS百科 bound its dependency property TextValue to a View Model RightSpecGlassStrength

UserControl code

<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
}

My EntityViewModel.cs is implementing INotifyDataErrorInfo interface and inherit ViewModelBase class

 public class EntityViewModel : ViewModelBase, INotifyDataErrorInfo
    {
      }

ViewModelBase.cs implements INotifyPropertyChanged

 public class ViewModelBase : INotifyPropertyChanged
    {
}

My code works fine when I bind the textbox or other silverlight control to the viewmodel. and shows proper validation exception on the control.

But when the user control gets a validation exception the control does not show any exception.

I'm not getting whats wrong with user control.???


It's difficult to fully debug the issue without the code behind for the user control, however, it seems to me that you have a dependency property on the code behind like this:

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

You then seem to be using the TextInputStart, KeyDown, KeyUp events to capture changes to the underlying control. My hunch is that either: a) You're failing to update the TextValue property to the new value. b) Your code behind is interfering with the validation process.

I would suggest that, instead of code behind, you name the user control in the xaml; i.e.

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

Then bind the text value of the underlying TextBox directly to the dependency property, like this:

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

This should then allow the validation mechanism to proceed as normal.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜