数据表单范围验证不起作用

发布于 2024-10-01 01:48:56 字数 4366 浏览 3 评论 0原文

我试图提出验证错误: 1.当用户输入非数字的美国MSRP时,silverlight在离开该字段时显示错误 2. 但是,如果在同一字段中输入负数,即使指定了明确的范围,也不会显示错误
我需要改变什么?

另外,作为一个额外的问题,我应该在 XAML 中使用什么来读取值 Display(Name = "My Name is US MSRP:" 而不是显式指定其他内容

public class CalculatorParameters : INotifyPropertyChanged
{ 
      private double _usMsrp;

    public CalculatorParameters()
    { 
    }


    [Display(Name = "My Name is US MSRP:",
        Description = "The residual value is based on the US MSRP, even with Euro-Delivery")]
    [Range(0, 150000, ErrorMessage = "US MSRP must be a positive amount under $150,000")]
    public double UsMsrp
    {
        get { return _usMsrp; }
        set
        {
            _usMsrp = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("UsMsrp"));
        }
    } 

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

}

这是 XAML

 <UserControl x:Class="Silverlight.ConfigEnhanced.UcFinance"
       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:my="clr-namespace:Silverlight.ConfigEnhanced"
         xmlns:df="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" 
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       d:DesignHeight="650" d:DesignWidth="500"  >
        <UserControl.Resources>
            <my:CalculatorParameters x:Key="descriptor"/>
        </UserControl.Resources>
        <df:DataForm x:Name="df1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CurrentItem="{StaticResource descriptor}" CommandButtonsVisibility="None" AutoGenerateFields="False" >
            <df:DataForm.EditTemplate>
                <DataTemplate>
                    <Grid x:Name="LayoutRoot" Background="White">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="160"></RowDefinition>
                            <RowDefinition Height="20"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="270"></RowDefinition> 
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Row="2" Grid.Column="0">
                            <TextBlock Text="Leasing"  HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"  Margin="0,0,0,15" />
                            <df:DataField Label="Term in Months">
                                <TextBox   Name="txtBoxLeaseTermInMonths" Text="{Binding LeaseTermInMonths, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="Down Payment">
                                <TextBox   Name="txtBoxLeaseDownPayment" Text="{Binding LeaseDownPayment, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="Money Factor">
                                <TextBox   Name="txtBoxLeaseMoneyFactor" Text="{Binding MoneyFactor, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="US MSRP">
                                <TextBox   Name="txtBoxLeaseUsMsrp" Text="{Binding UsMsrp, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                      </StackPanel>
                    </Grid>
                </DataTemplate>
            </df:DataForm.EditTemplate>
        </df:DataForm>
    </UserControl>

I'm trying to raise validation errors:
1. when the user enter a US MSRP that is not a number, silverlight displays an error when leaving the field
2. However if in the same field I put a negative number, no error is displayed even though there is an explicit range specified
What do I need to change?

Also, as a bonus question, what am I supposed to use in the XAML to read the value Display(Name = "My Name is US MSRP:" rather than explicitly specifying something else

public class CalculatorParameters : INotifyPropertyChanged
{ 
      private double _usMsrp;

    public CalculatorParameters()
    { 
    }


    [Display(Name = "My Name is US MSRP:",
        Description = "The residual value is based on the US MSRP, even with Euro-Delivery")]
    [Range(0, 150000, ErrorMessage = "US MSRP must be a positive amount under $150,000")]
    public double UsMsrp
    {
        get { return _usMsrp; }
        set
        {
            _usMsrp = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("UsMsrp"));
        }
    } 

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

}

And here is the XAML

 <UserControl x:Class="Silverlight.ConfigEnhanced.UcFinance"
       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:my="clr-namespace:Silverlight.ConfigEnhanced"
         xmlns:df="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" 
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       d:DesignHeight="650" d:DesignWidth="500"  >
        <UserControl.Resources>
            <my:CalculatorParameters x:Key="descriptor"/>
        </UserControl.Resources>
        <df:DataForm x:Name="df1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CurrentItem="{StaticResource descriptor}" CommandButtonsVisibility="None" AutoGenerateFields="False" >
            <df:DataForm.EditTemplate>
                <DataTemplate>
                    <Grid x:Name="LayoutRoot" Background="White">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="160"></RowDefinition>
                            <RowDefinition Height="20"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="270"></RowDefinition> 
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Row="2" Grid.Column="0">
                            <TextBlock Text="Leasing"  HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold"  Margin="0,0,0,15" />
                            <df:DataField Label="Term in Months">
                                <TextBox   Name="txtBoxLeaseTermInMonths" Text="{Binding LeaseTermInMonths, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="Down Payment">
                                <TextBox   Name="txtBoxLeaseDownPayment" Text="{Binding LeaseDownPayment, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="Money Factor">
                                <TextBox   Name="txtBoxLeaseMoneyFactor" Text="{Binding MoneyFactor, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                            <df:DataField Label="US MSRP">
                                <TextBox   Name="txtBoxLeaseUsMsrp" Text="{Binding UsMsrp, Mode=TwoWay}" GotFocus="RecomputeLease"/>
                            </df:DataField>
                      </StackPanel>
                    </Grid>
                </DataTemplate>
            </df:DataForm.EditTemplate>
        </df:DataForm>
    </UserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文