【问题标题】:How to make ToolTips have rounded corners - WPF如何使工具提示具有圆角 - WPF
【发布时间】:2018-07-31 18:49:51
【问题描述】:

我创建了一个小型 WPF 应用程序,并且页面上有许多文本框。所有这些文本框都有圆角。

我使用了 MVVM 模式并实现了 IDataErrorInfo 接口来向用户显示错误。当其中一个文本框的边缘为空时,工具提示应显示为红色,我已成功完成。

我现在希望红色边缘也像文本框一样具有圆角。如图所示,红色边框显示为TextBox为空,红色边框需要有圆角半径。

<!-- Text box xaml code that is used to display the error and binding -->
    <TextBox Style="{StaticResource TextBoxBase}"
                             Name="FirstName"
                             Text="{Binding FirstName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Source={StaticResource CustomerObject}, ValidatesOnDataErrors=True}"/>


<!-- Code that changes the tool tip if it's null -->
<Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />

            </Trigger>
        </Style.Triggers>

【问题讨论】:

  • 我对 XAML 和 WPF 比较陌生,并且不确定如何具体做到这一点

标签: c# wpf xaml tooltip


【解决方案1】:

您可以为错误创建特定的工具提示样式:

这是一个自定义工具提示样式的示例,其中 CornerRadius = "4"

<Style x:Key="ErrorRoundedTooltip" TargetType="ToolTip">
  <Setter Property="OverridesDefaultStyle" Value="true" />
  <Setter Property="HasDropShadow" Value="True" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ToolTip">
        <Border Name="Border" CornerRadius="4"
                BorderThickness="1"
                Width="{TemplateBinding Width}"
                Height="{TemplateBinding Height}">
          <ContentPresenter Margin="4" HorizontalAlignment="Left" VerticalAlignment="Top" />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Content">
       <Setter.Value>
            <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding ErrorContent.ValidationMessage}" VerticalAlignment="Center"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Setter.Value>
    </Setter>
</Style>

<ToolTip x:Key="ErrorRepository" Style="{StaticResource ErrorRoundedTooltip}" />

<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{StaticResource ErrorRepository}" />
        </Trigger>
    </Style.Triggers>
</Style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多