- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 IDataErrorInfo 验证 TextBox 的内容。下面列表的来源是一个列表,每个项目都是显示的。当我把 ValidatesOnDataErrors=True
在 TextBox 上的文本绑定(bind)中,它没有按预期工作。我该怎么做呢?
<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>
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>
最佳答案
我觉得执行IDataErrorInfo
在 ViewModel 上比在 Model 上更合适。
因此,在您的情况下,您可以创建一个额外的 ViewModel(例如: MyModelViewModel
)并将其包含为 List<MyModelViewModel>
里面 MyViewModel
用作ItemsSource
.
通过MVVM,如果你觉得你应该有一个对应的View,你可以提取出DataTemplate
的ItemsControl
到一个新的 XMAL。
关于validation - 如何在 ItemsControl 内的 TextBox 上使用 ValidatesOnDataErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6362083/
有没有办法为我的自定义 DependencyProperty 将 ValidatesOnDataErrors 设置为 True,这样我就不必在每次绑定(bind)到它时都这样做?符合 this 的内容
我必须需要绑定(bind) ValidatesOnDataErrors 的值,以便可以在运行时确定。 这样做的原因是因为我并不总是想验证数据。 这是绑定(bind)的属性,所以我知道我 不能做这个:
我有自己的错误提供程序,不想使用任何内置的 WPF 内容。我有一个绑定(bind)到整数的文本框,我有 ValidatesOnExceptions=False 和 ValidatesOnDataErr
我正在尝试使用 IDataErrorInfo 验证 TextBox 的内容。下面列表的来源是一个列表,每个项目都是显示的。当我把 ValidatesOnDataErrors=True在 TextBox
有没有办法以编程方式设置 ValidatesOnDataErrors=True ? 我有一个从 Textbox 继承的自定义文本框类,如下所示: public class ActiveTextBox:
我正在开发一个使用 DataAnnotations 实现 MVVM 设计模式的应用程序。该应用程序是一个动态生成的页面列表。在其中一个页面上,我有 10 个必填字段和 2 个是/否单选按钮。这 10
在WPF验证中,以下内容有什么区别: ValidatesOnNotifyDataErrors = True ValidatesOnDataErrors = True NotifyOnValidatio
我是一名优秀的程序员,十分优秀!