如何在ItemsControl内的TextBox上使用ValidatesOnDataErrors

时间:2011-06-15 17:56:27

标签: validation silverlight-4.0 mvvm idataerrorinfo

我正在尝试使用IDataErrorInfo验证TextBox的内容。下面列表的来源是一个列表,每个项目都显示。当我将ValidatesOnDataErrors=True放在TextBox上的文本绑定中时,它没有按预期工作。我该怎么做?

<ItemsControl ItemsSource="{Binding Trainings}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel>
                    <TextBlock Text="{Binding MobileOperator}" />
                    <TextBlock Text="{Binding LastUpdate}"/>
                </StackPanel>
                <StackPanel>
                    <TextBlock Text="Number trained*" />                        
                    <!-- ValidatesOnDataErrors doesn't work here-->
                    <TextBox 
                        Text="{Binding NumberTrained, 
                                       ValidatesOnDataErrors=True}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

更新:发布模型,ViewModel,View和CodeBehind的精简版

ViewModel和Model

public class MyViewModel : IDataErrorInfo, INotifyPropertyChanged
{
    public MyViewModel() 
    {
        Trainings = new List<MyModel>
        {
            new MyModel { NumberTrained = 5, MobileOperator = "MO 1", LastUpdate =         DateTime.Now },
            new MyModel { NumberTrained = 1, MobileOperator = "MO 2", LastUpdate = DateTime.Now },
        };

        OkButtonCommand = new ButtonCommand(OnClick);
    }

    private void OnClick()
    {
        PropertyChanged(this, new PropertyChangedEventArgs(""));
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public ICommand OkButtonCommand { get; private set; }
    public List<MyModel> Trainings { get; private set; }
    public string Error { get { return null; } }

    public string this[string columnName]
    {
        get
        {
            string error = null;
            switch (columnName)
            {
                case "NumberTrained":
                    error = "error from IDataErrorInfo";
                    break;
            }
            return error;
        }
    }
}

public class MyModel
{
    public string MobileOperator { get; set; }
    public DateTime LastUpdate { get; set; }
    public int NumberTrained { get; set; }
}

public class ButtonCommand : ICommand
{
    private Action _handler;
    public event EventHandler CanExecuteChanged;

    public ButtonCommand(Action handler) { _handler = handler; }
    public bool CanExecute(object parameter) { return true; }
    public void Execute(object parameter) { _handler(); }
}

背后的代码

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        DataContext = new MyViewModel();
    }
}

查看

<Canvas x:Name="LayoutRoot" Background="White">
    <ItemsControl ItemsSource="{Binding Trainings}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel HorizontalAlignment="Center">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <TextBlock Text="{Binding MobileOperator}" Margin="15,15,0,0" FontWeight="Bold"/>
                        <TextBlock Text="{Binding LastUpdate, StringFormat=' - Last Updated: \{0:M/d/yy\}'}" 
                                   Margin="5,15,15,0" Foreground="Gray"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <TextBlock Text="Number trained*" />
                        <TextBox Width="50" Height="20" 
                                 Text="{Binding NumberTrained, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <Button Content="ok" Width="100" Height="20" Canvas.Left="248" Canvas.Top="207" Command="{Binding OkButtonCommand}"/>
</Canvas>

2 个答案:

答案 0 :(得分:4)

我觉得在ViewModel上实现IDataErrorInfo比在Model上更合适。

因此,在您的情况下,您可以创建一个额外的ViewModel(例如:MyModelViewModel)并将其作为List<MyModelViewModel>包含在MyViewModel内,以用作ItemsSource

通过MVVM,如果您认为应该有相应的视图,则可以将DataTemplate的{​​{1}}提取到新的XMAL。

答案 1 :(得分:3)

您需要在模型上实现IDataErrorInfo,而不是ViewModel。

就像现在一样,当您尝试验证不存在的属性MyViewModel.NumberTrained时,您的验证检查会抛出错误,因此永远不会调用验证错误。