2016-09-21 100 views
1

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

我使用How can I add a hint text to WPF textbox?使它在单个TextBox上工作。现在我想让这个可重用(创建一个CustomControlWPF)。我也试图把它变成一个全球性的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到TextChanged。 一旦Text被清除,我想Preview显示出来。

为了把事情说清楚:

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

+1

这就是所谓的水印。看到这个问题。 ^^ –

+0

@LynnCrumbling很好...水印的故事是一个解决方案,让它工作。正如我上面所描述的,我已经使用'Syles'工作。您提供的“重复”不是关于创建自定义控件。 –

+1

我在该问题上看到多个答案,使用文本框派生的控件提供解决方案。我认为你没有检查所有的答案。 –

回答

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>