2011-07-19 12 views
3

私はツリービュー内のアイテムを検証しようとしています。主な考え方は、ユーザーがツリーからオブジェクトを選択し、そのオブジェクトが編集可能な詳細をロードすることです。 INotifyPropertyChangedが接続されているので、それはすべてうまくいきますが...私のモデルは検証ロジックを持っています(IDataErrorInfo)、ツリービューにも表示したいと思います(検証エラーがある項目を強調表示します) )。treeview item validation

私はすでにいくつかのことを試してきましたが、私が好きなように動作させるためにバリデーションをどこに置くべきかわかりません。

私の検証のControlTemplate:

<ControlTemplate x:Key="validationTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <AdornedElementPlaceholder x:Name="MyAdorner" /> 
       <Image 
              MaxHeight="{Binding ElementName=MyAdorner, Path=ActualHeight}" 
              MaxWidth="20" 
              Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}" 
              Margin="1" RenderOptions.BitmapScalingMode="HighQuality" 
              VerticalAlignment="Center" HorizontalAlignment="Center" /> 
      </StackPanel> 
     </ControlTemplate> 

ツリービュー:基礎となるモデルは、検証エラーを持っている場合、私は、ツリービューでアイテムの横に小さなアイコンを置くしようとしている。基本的

<TreeView ItemsSource="{Binding Path=ProductCategories}" 
       Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">   
       <TreeView.ItemTemplate> 
        <HierarchicalDataTemplate DataType="{x:Type data:ProductCategory}" 
             ItemsSource="{Binding Path=ProductCategories}"> 
         <StackPanel Orientation="Horizontal"> 
          <StackPanel.Style> 
           <Style TargetType="StackPanel"> 
            <Style.Triggers> 
             <Trigger Property="Validation.HasError" Value="True"> 
              <Setter Property="ToolTip" 
                Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> 
              <Setter Property="Validation.ErrorTemplate" 
                Value="{StaticResource validationTemplate}" /> 
             </Trigger> 
            </Style.Triggers> 
           </Style> 
          </StackPanel.Style> 
          <StackPanel.BindingGroup> 
           <BindingGroup /> 
          </StackPanel.BindingGroup> 
          <StackPanel.ToolTip> 
           <TextBlock Margin="2" Text="{Binding Path=Description}" /> 
          </StackPanel.ToolTip> 
          <TextBlock Text="{Binding Path=Name}" FontSize="10" FontWeight="Medium" /> 
          <TextBlock Text="{Binding Path=ProductCount, StringFormat=' ({0})'}" /> 
         </StackPanel> 
        </HierarchicalDataTemplate> 
       </TreeView.ItemTemplate> 
      </TreeView> 

私はBindingGroupで遊んでみましたが、それは私の新しいトピックです。だから私はそれが行く方法であるか分かりません。

アイデア?

答えて

1

まず、次のようなあなたのデータクラスのプロパティのカップルを必要とする:

public ObservableCollection<string> Errors 
{ 
    get 
    { 
     ObservableCollection<string> errors = new ObservableCollection<string>(); 
     errors.AddUniqueIfNotEmpty(this["PropertyToValidate1"]); 
     errors.AddUniqueIfNotEmpty(this["PropertyToValidate2"]); 
     ... 
     errors.AddUniqueIfNotEmpty(this["PropertyToValidateN"]); 
     return errors; 
    } 
} 

この最初のものは、インデクサがIDataErrorInfoのために加え呼び出し、そうでない場合は、コレクションにすべて検証エラーを追加しますすでにそこにいる。 AddUniqueIfNotEmptyメソッドは拡張メソッドであり、 'スズについては何をしているのですか'です。私はあなたがこの部分を自分で管理できると信じています。このコレクションの素晴らしい点は、単純なIDataErrorInfoの実装のように、一度に1つだけでなく、すべての検証エラーのコレクションをバインドして参照できることです。

public bool HasError 
{ 
    get { return Errors != null && Errors.Count > 0; } 
} 

第二の特性は、単にデータオブジェクトかに検証エラーがあるかどうかを示しbool返します。

次に、TemplateまたはStyleでこのプロパティにバインドすることができます。関連するデータオブジェクトに検証エラーがあった場合、Borderの色を変更する例を次に示します。

<Style x:Key="ValidationBorderStyle" TargetType="{x:Type Border}"> 
    <Setter Property="Border.BorderBrush" Value="Black" /> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding HasError, FallbackValue=False}" Value="True"> 
      <Setter Property="Border.BorderBrush" Value="Red" /> 
     </DataTrigger> 
    </Style.Triggers> 
</Style>