2016-10-12 74 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

當然,我給你一個完整的實現,但只有「價值」屬性。做您要驗證所有其他屬性相同:

1.Translation模型IDataErrorInfo的接口實現:

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。 Xaml在Value TextBox上帶有ValidatesOnDataErrors:

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

4.That會顯示一個紅色框周圍的空的文本框,如果你想顯示錯誤messeage徘徊溫習文本框,當你需要一個工具提示:

<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

很好的解決!但是當只有一個值爲空時,我不需要顯示錯誤。當他們都是空的時候我想要出錯。 – Ksice