【问题标题】:TextPreview on WPF Textbox [duplicate]WPF文本框上的TextPreview [重复]
【发布时间】:2017-01-29 16:38:05
【问题描述】:

Xamarin 上有一个名为Entry 的控件。它支持TextPreview,这就像一个默认文本,当文本框为空时显示在“背景”中。

我使用How can I add a hint text to WPF textbox? 让它在单个TextBox 上工作。现在我想让它可重用(在WPF 中创建一个CustomControl)。我还尝试将它打造为一个全球性的Stylehere,但我并没有真正得到我想要的。 -

长话短说:我怎样才能让这个 CustomControl 工作?

我没有比这更进一步的了:

public class TextboxWithPreview : TextBox
{
    public TextboxWithPreview()
    {
        if(DesignerProperties.GetIsInDesignMode(this))
        {
            this.TextPreview = "Default TextPreview";
        }
        EventManager.RegisterClassHandler(typeof(TextboxWithPreview), TextChangedEvent, new TextChangedEventHandler(OnTextChanged));    
    }

    public static readonly DependencyProperty TextPreviewProperty = DependencyProperty.Register("TextPreview", typeof(string), typeof(TextboxWithPreview));

    private static void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        //pseudo Code:
        if(string.IsNullOrEmpty(this.Text))
        {
            this.Text = TextPreview;
            this.ForeColor = Colors.Gray;
        }
    }

    public string TextPreview
    {
        get { return (string)GetValue(TextPreviewProperty); }
        set { SetValue(TextPreviewProperty, value); }
    }        
}

我对此的看法:

是否可以将第二个事件注册到现有属性? 如果是这样,我想将我的第二个 EventHandler 附加到TextChangedText 被清除后,我希望 Preview 出现。

说清楚:

我想创建一个 CustomControl - 没有变通方法。 由于它是在Xamarin.Forms.Entry 中实现的,因此它绝对是可能的。

【问题讨论】:

  • 这称为水印。看到这个问题。 ^^
  • @LynnCrumbling 很好...... Watermark 的故事是让它工作的一个解决方案。正如我上面所描述的,我已经使用Syles 让它工作了。您提供的“副本”与创建自定义控件无关。
  • 我看到关于该问题的多个答案提供了使用文本框派生控件的解决方案。我不认为你检查了所有的答案。
  • @LynnCrumbling 是的,我看到了。不幸的是,答案有点旧(2011 年)......那里没有提供框架(.Net 4.0 ?!或其他)。正如 cmets 所说,所提供的方法不起作用......这意味着......我仍然没有得到我需要的答案;( - 但我真的很感谢你的努力。

标签: c# wpf textbox custom-controls


【解决方案1】:

通过设置现有的 Text 属性,您将面临一场艰苦的战斗。在 TextBox 上放置标签并更改其可见性可能更容易。

CustomControl 通常使用ControlTemplate 设置样式,此模板应位于themes/Generic.xaml 中。

TextboxWithPreview.cs

public class TextboxWithPreview : TextBox
{
    public static DependencyProperty TextPreviewProperty = DependencyProperty.Register("TextPreview", typeof(string), typeof(TextboxWithPreview));

    public string TextPreview
    {
        get { return (string)GetValue(TextPreviewProperty); }
        set { SetValue(TextPreviewProperty, value); }
    }

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

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Local="YourNamespace">

    <!-- Describes how to style a TextboxWithPreview-->
    <Style x:Key="{x:Type Local:TextboxWithPreview}" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type Local:TextboxWithPreview}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Local:TextboxWithPreview}">
                    <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="TextPreview" Content="{Binding TextPreview, 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="TextPreview" Value="Hidden" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多