У меня есть элемент ItemsControl для отображения текстовых полей. Я хочу настроить проверку, поэтому, если все переводы пустые, произошла ошибка, а поля отмечены как «ошибка». Есть ли возможность сделать это?Проверка элементов управления в wpf не работает
Мой XAML: класс
<ItemsControl x:Name="LanguageItemsControl" ItemsSource="{Binding Path=Translations, Mode=TwoWay}"
LostFocus="OnLostFocus" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="5,2,5,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="47*"/>
<ColumnDefinition Width="53*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="ItemLabel" VerticalAlignment="Center"
Text="{Binding Path=Key, StringFormat={x:Static res:Resources.lblCaption}}" />
<TextBox Grid.Column="1" x:Name="ItemText" VerticalAlignment="Center"
HorizontalAlignment="Stretch" Margin="2,0,22,0"
Text="{Binding Path=Value, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
LostFocus="OnLostFocus"
AcceptsReturn="True"
MaxLines="2"
ScrollViewer.VerticalScrollBarVisibility="Auto"
MaxLength="150">
</TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
моей модели реализует от IDataErrorInfo
и INotifyPropertyChanged
Переводы является ObservableCollection
пользовательского типа «LanguageValue
» с открытыми свойствами, ключ и значение.
У меня была модель string this[string columnName]
, которая отлично работает с простыми текстовыми полями (за пределами ItemsControl), но как это сделать с моими товарами? У меня есть что-то вроде:
public string this[string columnName]
{
get
{
string result = null;
...
if (columnName == "Translations" || columnName == "ItemText")
{
if (Translations.All(t => string.IsNullOrEmpty(t.Value)))
result = Properties.Resources.errMsgEnterName;
}
...
Но, конечно, это не сработало.
Любые предложения?
Хорошее решение! Но мне не нужно показывать ошибку, когда только одно значение пусто. Я хочу ошибку, когда все они пусты. – Ksice