c# wpf xaml binding textbox

c# - TextPreview para Textbox



wpf xaml (1)

Para el cuadro de texto reutilizable, debe crear un control personalizado. También para el enlace no funciona bien con el pincel visual, por lo que necesita algún objeto temporal para almacenar el valor. Consulte mi código a continuación.

<Window x:Class="ChkList_Learning.Window4" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:ChkList_Learning" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="Window4" Height="300" Width="300"> <Window.Resources> <local:Temp x:Key="temp" Value="{Binding ElementName=Hostname, Path=Watermark}"/> <Style TargetType="{x:Type local:WatermarkTextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> <Style.Resources> <VisualBrush x:Key="WatermarkBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> <VisualBrush.Visual> <TextBlock Text="{Binding Source={StaticResource temp}, Path=Value}" FontFamily="Segoe UI" FontSize="20" Foreground="LightGray" Padding="5" /> </VisualBrush.Visual> </VisualBrush> </Style.Resources> <Style.Triggers> <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> <Setter Property="Background" Value="{StaticResource WatermarkBrush}" /> </Trigger> <Trigger Property="Text" Value="{x:Null}"> <Setter Property="Background" Value="{StaticResource WatermarkBrush}" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="Background" Value="White" /> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <local:WatermarkTextBox x:Name="Hostname" Height="40" FontFamily="Segoe UI" FontSize="20" VerticalContentAlignment="Center" Watermark="Hello, world."> </local:WatermarkTextBox> </Grid> </Window> public class Temp : Freezable { // Dependency Property public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(Temp), new FrameworkPropertyMetadata(string.Empty)); // .NET Property wrapper public string Value { get { return (string)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } protected override System.Windows.Freezable CreateInstanceCore() { return new Temp(); } } public class WatermarkTextBox : TextBox { static WatermarkTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(WatermarkTextBox), new FrameworkPropertyMetadata(typeof(WatermarkTextBox))); } public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBox)); public string Watermark { get { return (string)GetValue(WatermarkProperty); } set { SetValue(WatermarkProperty, value); } } }

Pregunta reelaborada para aclarar mis necesidades:

Quiero agregar una vista previa de Texto a Textboxes texto cuando están vacíos, al igual que algunos de ustedes lo saben de Xamarin .

Encontré esta respuesta en SO .

Este es el Style de la Respuesta que he vinculado anteriormente.

<TextBlock Grid.Row="5" Grid.Column="1" VerticalAlignment="Center" Text="Username:"> </TextBlock> <TextBox Grid.Row="5" Grid.Column="3"> <TextBox.Style> <Style TargetType="TextBox"> <Style.Resources> <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> <VisualBrush.Visual> <Label Content="Test" Foreground="LightGray" /> </VisualBrush.Visual> </VisualBrush> </Style.Resources> <Style.Triggers> <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> </Trigger> <Trigger Property="Text" Value="{x:Null}"> <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="Background" Value="White" /> </Trigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox>

Obtengo el siguiente resultado:

Como esto funciona bien, quiero aplicarlo a cada TextBox de TextBox en esa Window . Así que mi enfoque fue cambiar esta línea: <Label Content="Test" Foreground="LightGray" />

Pensé que tal vez cambiarlo a <Label Content="Test" Foreground="LightGray" /> haría el truco, pero no está funcionando.

Supongo que es algo con Tag Property y el Type of it (object instead of string) .

Dado que el primer enfoque funciona como el encanto, realmente no veo por qué debería necesitar un control personalizado para eso ...

Entonces, lo que probé es esto:

<Window.Resources> <Style TargetType="TextBox"> <Style.Resources> <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> <VisualBrush.Visual> <Label Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Foreground="LightGray" /> </VisualBrush.Visual> </VisualBrush> </Style.Resources> <Style.Triggers> <Trigger Property="Text" Value="{x:Static sys:String.Empty}"> <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> </Trigger> <Trigger Property="Text" Value="{x:Null}"> <Setter Property="Background" Value="{StaticResource CueBannerBrush}" /> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="Background" Value="White" /> </Trigger> </Style.Triggers> </Style> </Window.Resources>

¿Qué me estoy perdiendo? ¿Por qué no funciona?