MVVM模式中的WPF模型验证

时间:2015-05-05 06:44:54

标签: c# wpf validation xaml mvvm

到目前为止,我已经阅读过WPF验证规则和不同方法(使用IDataErrorInfoINotifyDataErrorInfoDataAttributes等),但没有一个符合我的需求。我想有一个模型验证器来验证整个视图模型(如果属性具有正值而另一个字段设置为1,我需要设置错误)

处理此方案的最佳方法是什么?

由于

更新#1:我忘了在Framework 4.0下说我没有可能升级到4.5

更新#2:我创建了一个较小的项目

这是我的观点:

<UserControl x:Class="TaskException.Views.SampleView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         mc:Ignorable="d" 
         >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

    <ComboBox Grid.Row="0" ItemsSource="{Binding DropdownList}" DisplayMemberPath="Description" SelectedValuePath="Id" SelectedValue="{Binding Obj.Sign}">

    </ComboBox>
    <telerik:RadMaskedNumericInput Grid.Row="1" Value="{Binding Obj.Qty, ValidatesOnDataErrors=True}"></telerik:RadMaskedNumericInput>

</Grid>

我的ViewModel:

public class SampleViewModel : Screen
{

    private List<DropDownOggetto> dropdownList;

    private DropDownOggetto selectedDropDownList;

    private Dummy obj;

    public DropDownOggetto SelectedDropDownList
    {
        get
        {
            return this.selectedDropDownList;
        }
        set
        {
            this.selectedDropDownList = value;
            NotifyOfPropertyChange(()=>SelectedDropDownList);
        }
    }

    public List<DropDownOggetto> DropdownList
    {
        get
        {
            return this.dropdownList;
        }
        set
        {
            this.dropdownList = value;
            NotifyOfPropertyChange(()=>DropdownList);
        }
    }

    public Dummy Obj
    {
        get
        {
            return this.obj;
        }
        set
        {
            this.obj = value;
            NotifyOfPropertyChange(() => Obj);
        }
    }

    public SampleViewModel()
    {
        Obj = new Dummy
        {
            Qty = 2000,
            Sign = 0
        };

        var lst = new List<DropDownOggetto>();

        lst.Add(new DropDownOggetto { Id = 0, Description = "Buy" });
        lst.Add(new DropDownOggetto { Id = 1, Description = "Sell" });

        DropdownList = lst;
    }

}

模特

public class DropDownOggetto
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public class Dummy : IDataErrorInfo 
{
    public int Sign { get; set; }

    public double Qty { get; set; }

    public string Error
    {
        get { return "Some kind of error";}
    }

    public string this[string columnName]
    {
        get
        {
            if (columnName == "Qty")
            {
                if (Sign == 0 && Qty < 0)
                    return "Qty must be positive";
                else if(Sign ==1 && Qty >=0)
                {
                    return "Qty must be negative";
                }
            }


            return null;
        }
    }
}

这个简单的样本工作得很好,除了如果我在RadMaskedNumericInput中有正数并且我更改了下拉列表,下面的属性上的验证没有被调用...我怎么能发信号呢?我可以将Sign Qty自动属性转换为属性并通知它们吗?

由于

0 个答案:

没有答案