代码之家  ›  专栏  ›  技术社区  ›  ErocM

WPF/C IDataErrorInfo未触发

  •  1
  • ErocM  · 技术社区  · 14 年前

    我的表单上有一个组合框和按钮。组合框中有类别。如果它们是基于布尔值的“系统类别”,则允许/禁止挂起。

    这是我的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
    

    有什么建议吗?

    谢谢,

    厄洛克

    1 回复  |  直到 14 年前
        1
  •  3
  •   olli-MSFT    14 年前

    索引器( public string this[string 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
    

    }