2012-12-19 46 views
2

我正在嘗試製作用於TextBoxes的自定義工具提示控件。嘗試將驗證傳遞給我的自定義工具提示控件

它看起來是這樣的:

enter image description here

......除了那些來自於我已離開,不論是否供儘可能好的背景組件一些像素。

的想法來源於: How to implement Balloon message in a WPF application

的問題是,我的自定義控制後面的代碼從來沒有得到驗證的對象(即應通過觸發器被傳遞給它的generic.xaml)。

爲什麼不呢?

generic.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> 

正如你看到的,我嘗試使用結核病的ElementName指驗證。 看起來像名稱不在模板中使用。如果我更改爲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> 

回答

2

不是按名稱提到的,通過使用RelativeSource結合得到的文本框本身。
嘗試類似這樣:

<Setter Property="ToolTip"> 
    <Setter.Value> 
     <Windows:ValidationBalloonPopupWindow 
      Validation="{Binding RelativeSource={RelativeSource Self}, Path=Validation}" /> 
    </Setter.Value> 
</Setter> 
相關問題