2016-10-12 5 views
0

翻訳テキストフィールドを表示するItemsControlがあります。 検証をセットアップしたいので、すべての翻訳が空であればエラーがあり、フィールドには「エラー」とマークされています。 これを行う可能性はありますか?wpfでItemsControlを検証できませんでした

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

私のモデルのクラスがIDataErrorInfoINotifyPropertyChanged 翻訳から実装は、パブリックプロパティキーと値を持つカスタムタイプ「LanguageValue」のObservableCollectionです。

私のモデルは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; 
    } 
... 

もちろんこれは動作しませんでした。

提案がありますか?

答えて

2

確かに、私はあなたに完全な実装を提供していますが、 "値"プロパティのみを使用しています。 IDataErrorInfoインターフェイスの実装と

1.Translationモデル::あなたのViewModelで

public class Translation : BindableBase, IDataErrorInfo 
{ 
    public string Value { get; set; } 

    public string this[string propertyName] 
    { 
     get 
     { 
      return GetErrorForPropery(propertyName); 
     } 
    } 

    public string Error { get; } 

    private string GetErrorForPropery(string propertyName) 
    { 
     switch (propertyName) 
     { 
      case "Value": 

       if (string.IsNullOrEmpty(Value)) 
       { 
        return "Please enter value"; 
       } 

       return string.Empty; 

      default: 
       return string.Empty; 
     } 
    } 
} 

2.Initialize翻訳:

public ObservableCollection<Translation> Translations { get; set; } 

    public MainViewModel() 
    { 
     Translations = new ObservableCollection<Translation> 
     { 
      new Translation {Value = "A"}, 
      new Translation(), 
      new Translation {Value = "C"} 
     }; 
    } 

3あなたが検証したい他のすべてのプロパティと同じ操作を行います。値のValidatesOnDataErrorsを持つXaml TextBox:

<ItemsControl x:Name="LanguageItemsControl" ItemsSource="{Binding Path=Translations, Mode=TwoWay}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="5,2,5,2"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="47*"/> 
         <ColumnDefinition Width="53*"/> 
        </Grid.ColumnDefinitions> 
        <TextBox Grid.Column="0" x:Name="ItemLabel" VerticalAlignment="Center" Text="{Binding Path=Value, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:customControlLib="clr-namespace:CustomControlLib;assembly=CustomControlLib" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    DataContext="{StaticResource MainViewModel}"> 
<Window.Resources> 
    <Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
          Value="{Binding RelativeSource={RelativeSource Self}, 
          Path=(Validation.Errors)[0].ErrorContent}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <ItemsControl x:Name="LanguageItemsControl" ItemsSource="{Binding Path=Translations, Mode=TwoWay}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="5,2,5,2"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="47*"/> 
         <ColumnDefinition Width="53*"/> 
        </Grid.ColumnDefinitions> 
        <TextBox Grid.Column="0" x:Name="ItemLabel" Style="{StaticResource TextBoxStyle}" VerticalAlignment="Center" Text="{Binding Path=Value, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl>  

+0

良い解決策:4.Thatは、テキストボックスをオーバーザホバリングするときにエラーmesseageを表示したい場合は、ツールチップを必要とし、空のテキストボックスの周りに赤いボックスが表示されます!しかし、1つのValueだけが空の場合はエラーを表示する必要はありません。私はそれらのすべてが空の時にエラーが欲しいです。 – Ksice

関連する問題