2011-09-24 78 views
0

下面的控件背後的想法是,當沒有任何文本時,能夠在文本框內顯示標籤。爲了做到這一點,我只是採用了一個類似於SearchTextBox的工作控件,但是我在這裏需要的東西太多了。自定義控件未呈現

我懷疑下面的風格的控制模板,但我沒有看到任何明顯的錯誤。我可以很容易地錯過某些對於有更多經驗的人來說顯而易見的東西。

如何修復或故障排除?

乾杯,
Berryl

代碼

/// <summary>Light weight version of a <see cref="SearchTextBox"/></summary> 
public class LabelTextBox : TextBox 
{ 

    #region Dependency Properties 

    #region Label Text 

    /// <summary>Sets the 'watermark' text that acts as a label, identifying what this will search for.</summary> 
    public static readonly DependencyProperty LabelTextProperty = 
     DependencyProperty.Register("LabelText", 
      typeof (string), typeof (LabelTextBox), new PropertyMetadata("Search")); 

    public string LabelText 
    { 
     get { return (string) GetValue(LabelTextProperty); } 
     set { SetValue(LabelTextProperty, value); } 
    } 

    /// <summary>Brush for the label text.</summary> 
    public static readonly DependencyProperty LabelTextColorProperty = 
     DependencyProperty.Register("LabelTextColor", typeof (Brush), typeof (LabelTextBox)); 

    public Brush LabelTextColor 
    { 
     get { return (Brush)GetValue(LabelTextColorProperty); } 
     set { SetValue(LabelTextColorProperty, value); } 
    } 

    #endregion 

    #region Has Text 

    private static readonly DependencyPropertyKey HasTextPropertyKey = 
     DependencyProperty.RegisterReadOnly("HasText", 
      typeof (bool), typeof (LabelTextBox), new PropertyMetadata()); 

    /// <summary>True if the user has typed in text.</summary> 
    public static readonly DependencyProperty HasTextProperty = HasTextPropertyKey.DependencyProperty; 

    public bool HasText 
    { 
     get { return (bool)GetValue(HasTextProperty); } 
     private set { SetValue(HasTextPropertyKey, value); } 
    } 

    #endregion 

    #endregion 

    #region Construction 

    static LabelTextBox() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof (LabelTextBox), new FrameworkPropertyMetadata(typeof (LabelTextBox))); 
    } 

    #endregion 

    #region Event Handlers 

    protected override void OnTextChanged(TextChangedEventArgs e) 
    { 
     base.OnTextChanged(e); 

     HasText = Text.Length != 0; 
    } 

    #endregion 

} 

風格

<Style x:Key="{x:Type cc:LabelTextBox}" TargetType="{x:Type cc:LabelTextBox}"> 
    <Setter Property="Background" Value="{StaticResource SearchTextBox_Background}" /> 
    <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_Border}" /> 
    <Setter Property="Foreground" Value="{StaticResource SearchTextBox_Foreground}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 
    <Setter Property="LabelText" Value="Search" /> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="LabelTextColor" Value="{StaticResource SearchTextBox_LabelTextColor}" /> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type cc:LabelTextBox}"> 

       <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Padding="2" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Background="{TemplateBinding Background}" 
         > 

        <Grid x:Name="LayoutGrid"> 

         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualHeight}" /> 
         </Grid.ColumnDefinitions> 

         <ScrollViewer x:Name="PART_ContentHost" Grid.Column="0" /> 

         <Label x:Name="LabelText" Grid.Column="0" 
           Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelTextColor}" 
           Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelText}" 
           Padding="2,0,0,0" FontStyle="Italic" 
           /> 

        </Grid> 

       </Border> 

       <ControlTemplate.Triggers> 

        <!--input triggers (mouse over && keyboard focused--> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_BorderMouseOver}" /> 
        </Trigger> 
        <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
         <Setter Property="BorderBrush" Value="{StaticResource SearchTextBox_BorderMouseOver}" /> 
        </Trigger> 

        <!--Once the user has typed in search text--> 

        <Trigger Property="HasText" Value="True"> 
         <!--Hide the label text--> 
         <Setter Property="Visibility" TargetName="LabelText" Value="Hidden" /> 
        </Trigger> 

       </ControlTemplate.Triggers> 

      </ControlTemplate> 
     </Setter.Value> 

    </Setter> 

    <!-- 
    Error display 
    --> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <DockPanel LastChildFill="True"> 
        <TextBlock DockPanel.Dock="Right" Text=" *" 
           Foreground="Red" 
           FontWeight="Bold" FontSize="16" 
           ToolTip="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"/> 
        <Border BorderBrush="Red" BorderThickness="1"> 
         <AdornedElementPlaceholder Name="placeholder"></AdornedElementPlaceholder> 
        </Border> 
       </DockPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="True"> 
      <Setter Property="Background" Value="LightYellow"/> 
     </Trigger> 
    </Style.Triggers> 


</Style> 

回答

1

這似乎是工作。我所做的只是將代碼複製到名爲LabelTextBox的類中,並將StyleGeneric.xaml資源字典中的Themes文件夾放置在項目根目錄下(我將刷子顏色更改爲LightBlue,LightGray,Blue等)