Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, this may seem weird at first glance, but I believe that it's the right way to do that. I derived from a <code>TextBox</code> and added <code>ContentPresenter</code> there to host any content and the <code>Content</code> property to set that content. </p> <p>So, create WPF Custom Control (it's Visual Studio template) called TextBoxContainer. add the following property to it:</p> <pre><code> #region Content (DependencyProperty) /// &lt;summary&gt; /// Gets or sets Content property /// &lt;/summary&gt; public object Content { get { return (object)GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } } public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(TextBoxContainer), new PropertyMetadata(null)); #endregion </code></pre> <p>Replace it's style in Assets/generic.xaml file with the following:</p> <pre><code>&lt;LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"&gt; &lt;GradientStop Color="#ABADB3" Offset="0.05"/&gt; &lt;GradientStop Color="#E2E3EA" Offset="0.07"/&gt; &lt;GradientStop Color="#E3E9EF" Offset="1"/&gt; &lt;/LinearGradientBrush&gt; &lt;Style TargetType="{x:Type local:TextBoxContainer}"&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt; &lt;Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/&gt; &lt;Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/&gt; &lt;Setter Property="BorderThickness" Value="1"/&gt; &lt;Setter Property="Padding" Value="1"/&gt; &lt;Setter Property="AllowDrop" Value="true"/&gt; &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt; &lt;Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/&gt; &lt;Setter Property="Stylus.IsFlicksEnabled" Value="False"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:TextBoxContainer}"&gt; &lt;Grid&gt; &lt;Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"&gt; &lt;ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt; &lt;/Microsoft_Windows_Themes:ListBoxChrome&gt; &lt;ContentPresenter Content="{TemplateBinding Content}"/&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>And use it like following:</p> <pre><code>&lt;local:TextBoxContainer TextChanged="TextBoxGrid_TextChanged" &gt; &lt;local:TextBoxContainer.Content&gt; &lt;Grid&gt; &lt;!-- Any markup here --&gt; &lt;/Grid&gt; &lt;/local:TextBoxContainer.Content&gt; &lt;/local:TextBoxContainer&gt; </code></pre> <p>Note that at any time it has text that contains all text that was input. The only thing that you should be aware of is that some elements prevent some events from bubbling up. For example <code>TextBox</code> stops <code>KeyDown</code> event bubbling. So if you place <code>TextBox</code> inside <code>TextBoxContainer</code> and user will click on it and start to input text there - TextBoxContainer will not receive that text, although it will fire <code>TextChanged</code> event.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload