2010-11-24 7 views
1

フォームにコンボボックスとボタンがあります。コンボボックスにはカテゴリがあります。ブール値に基づいて 'システムカテゴリ'である場合は、保留中のものを許可/禁止します。WPF/C#IDataErrorInfo Not Firing

   <StackPanel Grid.Column="1" Grid.Row="1"> 
        <Label Content="Delete Category" Height="28"/> 
        <ComboBox x:Name="comboBox_DeleteCategory" 
           Grid.Row="1" 
           Height="29"         
           ItemsSource="{Binding Path=CategorySelected.Items, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" 
           SelectedItem="{Binding Path=CategorySelected.SelectedItem ,ValidatesOnDataErrors=True, NotifyOnValidationError=true}" 
           DisplayMemberPath="Name"/> 
        <Button Content="Delete" Height="25" Margin="0,5,0,0" HorizontalAlignment="Right" Width="103.307" Command="{Binding DeleteCommand}"/> 
       </StackPanel> 

と判断された場合、私は、ツールチップを表示するコンボボックスを取得しようとしています:これは、その中の2つのコントロールを持つスタックパネルがある

<Window.Resources> 
    <Style TargetType="{x:Type ComboBox}"> 
     <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> 

:ここ

は私のXAMLですこれはシステムカテゴリであることを示します。

DeleteCommandは正常に動作しているため、システムカテゴリでヒットしたときにボタンが無効になる問題は発生しません。

これは、ツールチップを表示するために私のコードです:

#region IDataErrorInfo Members 

public string Error { get; set; } 

public string this[string columnName] 
{ 
    get 
    { 
    Error = ""; 
    switch (columnName) 
    { 
     case "comboBox_DeleteCategory": 
     if (CategorySelected.SelectedItem != null && CategorySelected.SelectedItem.IsInternal) 
     { 
      Error = CategorySelected.SelectedItem.Name + " is an system category and cannot be deleted."; 
      break; 
     } 
     break; 

    } 

    return Error; 
    } 
} 

#endregion 

任意の提案ですか?

おかげで、

Eroc

答えて

3

インデクサ最新のバインディング更新によって変更されたプロパティ名で呼ばれている(この[文字列COLUMNNAME]が公共の文字列)。つまり、 "comboBox_DeleteCategory"(コントロール名)のフィルタリングはここでは役に立ちません。コントロールのバインディングによって更新されたプロパティをフィルタ処理し、それが期待どおりの状態にあるかどうかを判断する必要があります。インデクサーにブレークポイントを設定し、columnNameの値を見ることができます。さらに、エラープロパティは、WPFではまったく使用されません。したがって、設定する必要はありません。簡単な例:

public class Contact : IDataErrorInfo, INotifyPropertyChanged 
{ 
    private string firstName; 
    public string FirstName 
    { 
     // ... set/get with prop changed support 
    } 

    #region IDataErrorInfo Members 

    public string Error 
    { 
     // NOT USED BY WPF 
     get { throw new NotImplementedException(); } 
    } 

    public string this[string columnName] 
    { 
     get 
     { 
      // null or string.Empty won't raise a validation error. 
      string result = null; 

      if(columnName == "FirstName") 
      { 
       if (String.IsNullOrEmpty(FirstName)) 
        result = "A first name please..."; 
       else if (FirstName.Length < 5) 
        result = "More than 5 chars please..."; 
      } 

      return result; 
    } 
} 

#endregion 

}