1

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

这是我的xml:

<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

有什么建议么?

谢谢,

爱罗克

4

1 回答 1

3

索引器 ( public string this[string columnName] ) 使用已被最新绑定更新更改的属性名称调用。也就是说,过滤“comboBox_DeleteCategory”(控件名称)在这里没有帮助。您必须筛选由控件绑定更新的属性,并确定它是否处于预期状态。您可以在索引器中放置一个断点并观察 columnName 的值。更重要的是,WPF 根本不使用Error属性。因此,没有必要设置它。一个简单的例子:

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

}

于 2010-11-24T21:32:15.350 回答