2010-11-24 106 views
1

我在窗体上有一个组合框和按钮。组合框包含类别。如果它们是基于布尔值的“系统类别”,我想允许/禁止未决。WPF/C#IDataErrorInfo Not Firing

这是我的XAML:

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

这是两个控件栈面板在其中:

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

我试图让组合框来显示工具提示,如果确定它是一个系统类别。

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 

}