下面控件背后的想法是,当没有任何文本时,能够在TextBox内部显示一个Label。要做到这一点,我只是采用了一个类似的工作控件,一个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>
答案 0 :(得分:1)
这似乎有效。我所做的只是将您的代码复制到名为LabelTextBox
的类中,并将Style
复制到位于项目根目录的Generic.xaml
文件夹中的Themes
资源字典中(我更改了笔刷颜色) LightBlue,LightGray,Blue等。)