2

我正在尝试制作一个用于 TextBoxes 的自定义工具提示控件。

它看起来像这样:

在此处输入图像描述

...除了来自背景组件的一些像素,我已经尽可能好地去除了这些像素。

思路来自: How to implement Balloon message in a WPF application

问题是我的自定义控件背后的代码永远不会获得验证对象(应该通过 generic.xaml 中的触发器传递给它)。

为什么不?

通用的.xaml:

  <Style TargetType="{x:Type TextBox}" x:Name="tb">
        <Setter Property="Width" Value="200" />
        <Setter Property="Background" Value="{StaticResource InputBackgroundColor}" />
        <Setter Property="BorderBrush" Value="{StaticResource InputBorderBrush}" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Margin" Value="5,0,0,5" />
    <Style.Triggers>
      <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip">
          <Setter.Value>
            <Windows:ValidationBalloonPopupWindow
              Validation="{Binding Path=Validation, ElementName=tb}" />
          </Setter.Value>
        </Setter>
      </Trigger>
    </Style.Triggers>
  </Style>

如您所见,我尝试使用 tb 的 ElementName 来引用 Validation。似乎 Name 没有在模板中使用。如果我改为 x:Key,我所有的文本框都会变成 10 像素宽。换句话说,可能不是正确的做法。

ValidationBalloonPopupWindow.xaml.cs 背后的代码:

using System.Windows;
using System.Windows.Controls;

namespace Foo.ToolTips
{
    public partial class ValidationBalloonPopupWindow : ToolTip
    {
        public ValidationBalloonPopupWindow()
        {
            InitializeComponent();
        }    

        public static DependencyProperty ValidationProperty
            = DependencyProperty.Register("Validation", typeof(object), typeof(ValidationBalloonPopupWindow),
                new PropertyMetadata(null, OnChangedValidationByBinding));

        private static void OnChangedValidationByBinding
            (DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ValidationBalloonPopupWindow)d).OnChangedValidationByBinding(e.NewValue);
        }
        public void OnChangedValidationByBinding(object newValue)
        {
            txtMessage.Text = newValue.GetType().Name;
        }

        private object _validation;
        public object Validation
        {
            get
            {
                return _validation;
            }
            set
            {
                _validation = value;
                txtMessage.Text = _validation.GetType().Name;
            }
        }
    }
}

其中有一个应该运行的设置器,我试图在这个文件中放置很多断点但没有成功。

控件本身的 xaml,ValidationBalloonPopupWindow.xaml:

<ToolTip x:Class="FRAM.Windows.ValidationBalloonPopupWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Background="Transparent" BorderBrush="Transparent" HasDropShadow="false"
    Placement="Bottom"
    Height="Auto" Width="Auto">
  <Grid Height="126" Width="453">
    <Border Margin="7,13,0,0"
          CornerRadius="10,10,10,10" Grid.ColumnSpan="4" HorizontalAlignment="Left" Width="429" Height="82" VerticalAlignment="Top" Grid.RowSpan="2">
      <Border.Effect>
        <DropShadowEffect Color="#FF474747" />
      </Border.Effect>
      <Border.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
          <GradientStop Color="#FF58C2FF" Offset="0" />
          <GradientStop Color="#FFFFFFFF" Offset="1" />
        </LinearGradientBrush>
      </Border.Background>
      <StackPanel Orientation="Vertical">
        <Label Content="Title" Height="31" HorizontalAlignment="Left"
          Margin="12,8,0,0" Name="lblCaption" FontSize="16" FontWeight="Bold" />
        <TextBlock Margin="18,0,0,0" Name="txtMessage" Width="378" HorizontalAlignment="Left">Body</TextBlock>
      </StackPanel>
    </Border>
    <Path Data="M25,25L10.9919,0.64 0.7,25" Fill="#FF58C2FF" HorizontalAlignment="Left"
      Margin="32,3,0,0" Stretch="Fill" Width="22" Height="10" VerticalAlignment="Top" />
  </Grid>
</ToolTip>
4

1 回答 1

2

RelativeSource通过使用绑定来获取文本框本身,而不是通过名称引用。
尝试这样的事情:

<Setter Property="ToolTip">
    <Setter.Value>
       <Windows:ValidationBalloonPopupWindow
           Validation="{Binding RelativeSource={RelativeSource Self}, Path=Validation}" />
    </Setter.Value>
</Setter>
于 2012-12-19T15:41:22.577 回答