我想创建自定义文本框(它将包含简单的水印/提示文本),并且我遇到了依赖属性的问题。 我的自定义类继承自TextBox类,包含两个文件(.xaml .cs) C#代码文件非常简单:
public partial class HintTextBox : TextBox
{
public string HintText
{
get { return (string)GetValue(HintTextProperty); }
set { SetValue(HintTextProperty, value); }
}
public static readonly DependencyProperty HintTextProperty =
DependencyProperty.Register(
"HintText",
typeof(string),
typeof(HintTextBox),
new FrameworkPropertyMetadata(string.Empty));
}
.xaml文件包含显示/隐藏水印的代码
<local:HintTextBox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Ribes.Client.GUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<local:HintTextBox.Style>
<Style TargetType="{x:Type local:HintTextBox}">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding HintText, ElementName=local:HintTextBox}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers><!-- triggers changing content of control --></Style.Triggers>
</Style>
</local:HintTextBox.Style>
我正在尝试简单地使用我的HintTextBox:
<local:HintTextBox HintText="hint text"></local:HintTextBox>
它不起作用。 在线创建制动点后:
SetValue(HintTextProperty, value);
永远不会达到代码。 TextBox的继承属性(Text,Background)工作正常:
<local:HintTextBox Text="example text" Background="Yellow"></local:HintTextBox>
我们可以在里面找到带有“示例文本”的正确黄色TextBox。
有人知道我的DP有什么问题吗?
Regars NQ
答案 0 :(得分:1)
You will want to use Generic.xaml to style your textbox. To do this you will need to override the DefaultStyleKey in a static constructor.
HintTextBox.cs
public class HintTextBox : TextBox
{
public static DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(string), typeof(HintTextBox));
public string HintText
{
get { return (string)GetValue(HintTextProperty); }
set { SetValue(HintTextProperty, value); }
}
static HintTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HintTextBox), new FrameworkPropertyMetadata(typeof(HintTextBox)));
}
}
Generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local="Ribes.Client.GUI">
<!-- Describes how to style a HintTextBox -->
<Style x:Key="{x:Type Local:HintTextBox}" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type Local:HintTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Local:HintTextBox}">
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
<Grid x:Name="LayoutGrid">
<ScrollViewer x:Name="PART_ContentHost" Margin="2" />
<Label x:Name="HintText" Content="{Binding HintText, RelativeSource={RelativeSource TemplatedParent}}"
FontStyle="Italic" Margin="2" Padding="2,0,0,0" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasText" Value="True">
<Setter Property="Visibility" TargetName="HintText" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
答案 1 :(得分:0)
尝试这个并设置一个断点:
public string HintText
{
get { return (string)GetValue(HintTextProperty); }
set { SetValue(HintTextProperty, value); }
}
public static readonly DependencyProperty HintTextProperty =
DependencyProperty.Register(
"HintText",
typeof(string),
typeof(HintTextBox),
new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,(sender,e)=>{
//Set Break Point Here
});
答案 2 :(得分:-1)
我现在无法访问Visual Studio,但我认为您可能需要将FrameworkPropertyMetadata
替换为UIPropertyMetadata
到您的DependencyProperty中。