gpt4 book ai didi

c# - 依赖属性和自定义文本框的问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:15:19 25 4
gpt4 key购买 nike

我想创建自定义文本框(其中将包含简单的水印/提示文本)并且我遇到了依赖属性的一些问题。我的自定义类继承自 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 的继承属性(文本、背景)工作正常:

<local:HintTextBox Text="example text" Background="Yellow"></local:HintTextBox>

我们可以看到正确的黄色文本框,里面有“示例文本”。

有人知道我的 DP 有什么问题吗?

敬礼网易

最佳答案

您需要使用 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>

关于c# - 依赖属性和自定义文本框的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39252598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com