开发者

How to use ValidatesOnDataErrors on a TextBox inside an ItemsControl

I'm trying to have a TextBox's content be validated using IDataErrorInfo. The source of the list below is a List and each item is display. When i put ValidatesOnDataErrors=True in the Binding for the Text on the TextBox, it's not working as expected. How do I do this?

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

Update: Posting a stripped down version of Model, ViewModel, View and CodeBehind

ViewModel and 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(); }
}

Code Behind

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

        DataContext = new MyViewModel();
    }
}

View

<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>
          开发者_Python百科      </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <Button Content="ok" Width="100" Height="20" Canvas.Left="248" Canvas.Top="207" Command="{Binding OkButtonCommand}"/>
</Canvas>


I feel implementing IDataErrorInfo on ViewModel is more appropriate rather than on Model.

So in your case, you could have created an additional ViewModel (ex: MyModelViewModel) and include it as a List<MyModelViewModel> inside MyViewModel to be used as the ItemsSource.

Going by MVVM, if you feel you should have a corresponding View for it, you can extract out the DataTemplate of the ItemsControl to a new XMAL.


You need to implement IDataErrorInfo on your Model, not your ViewModel.

As it is right now, your a validation check is throwing an error when you try and validate the property MyViewModel.NumberTrained, which doesn't exist, so the validation error never gets called.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜