Validation.HasError工具提示内容

时间:2014-11-17 13:07:50

标签: c# wpf validation

我正在尝试创建自定义验证规则机制。我想在Validation.HasError为真时切换工具提示样式和内容。为此,我正在使用它:

<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip">
            <Setter.Value>
                <ToolTip
                    ToolTip="{Binding  Path=(Validation.Errors)[0].ErrorContent}"
                    Tag="{Binding  Path=(Validation.Errors)[0].ErrorContent, Converter={StaticResource TooltipJoin}}"
                    Style="{StaticResource WarningTooltip}"/>
            </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>

警告工具提示模板是:

<Border BorderThickness="1"
    BorderBrush="{DynamicResource FeedackBrush}"
    Background="{DynamicResource Color}"
    MinWidth="42" CornerRadius="2"
    Padding="5,3,5,3">

    <!--Holds the image and text for the tooltip-->
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <ContentPresenter
          Grid.Column="0"
          Content="{DynamicResource WarningIcon}"
          Width="32"
          Height="32"/>

        <!--Display the text-->
        <TextBox Background="Transparent"
             BorderThickness="0"
             VerticalAlignment="Center"
             Grid.Column="1"
             Foreground="{DynamicResource ForgroundColor}"
             Text="{TemplateBinding Content}"
             TextAlignment="Center"
             BorderBrush="Transparent"/>
    </Grid>
</Border>

验证方法:

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  {
     try
     {
        if (value != null)
        {
           var index = (int)value;
           if (index == 2 || index == 5)
           {
              return new ValidationResult(true, null);
           }
        }
     }
     catch (Exception e)
     {

        return new ValidationResult(false, e.Message);
     }

     return new ValidationResult(false, "Please select corect item");
  }

在xaml:

<ComboBox Name="MyCombo"
            Grid.Row="0" 
            Grid.Column="0" 
            BorderThickness="5"
            ItemsSource="{Binding MyItemsCollection,UpdateSourceTrigger=PropertyChanged}"
            Height="30" 
           Margin="50,178,50,42">
        <ComboBox.SelectedIndex>
            <Binding Path="Index" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules >
                    <c:ComboBoxRule ItemIdex="2" ValidatesOnTargetUpdated="True" />
                </Binding.ValidationRules>
            </Binding>
        </ComboBox.SelectedIndex>
</ComboBox>

我可以在常规样式和警告工具提示样式之间切换样式,但内容为空。有关内容问题的任何建议吗?

2 个答案:

答案 0 :(得分:1)

我相信这是你的问题。 ToolTip应设置Content,而不是ToolTip。此外,您需要使用BindingMode RelativeSource指定一些Self来定位样式元素,而不是使用隐式DataContext(来自您的viewmodel):

<Setter.Value>
      <ToolTip
           Content="{Binding Path=(Validation.Errors)[0].ErrorContent,
                             RelativeSource={RelativeSource Self}}"
           Tag="{Binding Path=(Validation.Errors)[0].ErrorContent, 
                 Converter={StaticResource TooltipJoin},
                 RelativeSource={RelativeSource Self}}"
           Style="{StaticResource WarningTooltip}"/>
</Setter.Value>

答案 1 :(得分:0)

以某种方式查找解决方案: 我把它放在触发器

 <Trigger Property="Validation.HasError" Value="true">
             <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip  Style="{StaticResource WarningTooltip}"/>
                    </Setter.Value>
             </Setter>
                <Setter Property="Tag" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent, Converter={StaticResource TooltipJoin}}"/>
            </Trigger>

对于文本框内容,请使用以下内容:

Content="{Binding Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Control}, AncestorLevel=2}}"