C# 依赖属性问题&;自定义文本框
我想创建自定义文本框(其中将包含简单的水印/提示文本),我遇到了一些依赖属性的问题。 我的自定义类继承自TextBox类,由两个文件(.xaml.cs)组成 C代码文件非常简单:C# 依赖属性问题&;自定义文本框,c#,wpf,xaml,C#,Wpf,Xaml,我想创建自定义文本框(其中将包含简单的水印/提示文本),我遇到了一些依赖属性的问题。 我的自定义类继承自TextBox类,由两个文件(.xaml.cs)组成 C代码文件非常简单: public partial class HintTextBox : TextBox { public string HintText { get { return (string)GetValue(HintTextProperty); } set { SetValue(
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>
代码永远不会到达。TextBox的继承属性(文本、背景)工作正常:
<local:HintTextBox Text="example text" Background="Yellow"></local:HintTextBox>
我们可以看到正确的黄色文本框,里面有“示例文本”
有人知道我的DP有什么问题吗
富豪
NQ尝试此操作并设置断点:
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
});
我现在无法访问Visual Studio,但我认为您可能需要将
FrameworkPropertyMetadata
替换为UIPropertyMetadata
,将其替换为您的DependencyProperty。您需要使用Generic.xaml来设置文本框的样式。为此,需要在静态构造函数中重写DefaultStyleKey
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>
如果我没记错的话,请参考我的答案。WPF不使用DP的包装器属性,它使用DP的SetValue
——这就是为什么未达到断点的原因。我怀疑绑定({binding HintText,ElementName=local:HintTextBox}
)是不正确的(我的意思是ElementName
),我认为这有点不幸。。。您给出了一个提示,如何监控DP中的更改,但没有描述为什么您的解决方案比问题代码更好。我有一种感觉,“有人知道我的DP可能有什么问题吗?”这个问题实际上不仅仅针对DP存储值,而且还针对HintText
的视觉表示,尽管没有明确提出这个问题。
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)));
}
}
<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>