2016-09-21 45 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>