wpf 当在文本框中键入内容或在构造函数中向文本框中添加文本时,如何删除文本框中的水印文本?

4sup72z8  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(109)

xaml中的文本框部分:

<TextBox x:Name="searchBox" HorizontalAlignment="Center" VerticalAlignment="Top" Width="1100" Height="30" Margin="10,70,0,0" FontSize="16" TextChanged="searchBox_TextChanged">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="MaxLength" Value="50" />
            <Setter Property="Foreground" Value="DarkGray" />
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
    <TextBox.Text>
        <Binding Path="SearchText" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
    </TextBox.Text>
    <TextBox.Template>
        <ControlTemplate TargetType="TextBox">
            <Grid>
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                <TextBlock Text="Hello World" Foreground="LightGray" Margin="5,0,0,0"/>
                <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Grid>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>

运行应用程序时,结果是文本框中的水印Hello World:

但是在MainWindow.xaml.cs的构造函数中,如果我添加这行:

public MainWindow()
{
    InitializeComponent();

    searchBox.Text = "aaa";
}

在文本框中运行应用程序时,我没有看到屏幕。
我希望,如果我在构造函数中添加这一行,它将覆盖Hello World,因此Hello World将被删除,而Hello World将被删除。但它一点都不显示出它的颜色。
第二件事是事件searchBox_TextChanged在这里我想当我在文本框中键入的东西,你好世界将被删除,现在当我键入的东西,它的显示我键入的文本,但也保持水印你好世界。

private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
    // Filter the ListView based on the search text
    string searchText = searchBox.Text.ToLower();
    var filteredProcesses = processDictionary.Values.Where(p => p.Name.ToLower().Contains(searchText)).ToList();
    lstvw.ItemsSource = filteredProcesses;
}
jm81lzqq

jm81lzqq1#

这是工作。
添加了一个新类:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace wpfMultiThreadListViewUpdate
{
    public class TextLengthToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int textLength && textLength > 0)
            {
                return Visibility.Collapsed; // Hide watermark when text is not empty
            }
            return Visibility.Visible; // Show watermark when text is empty
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

更新了xaml代码:

<Window x:Class="wpfMultiThreadListViewUpdate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:wpfMultiThreadListViewUpdate"
        Title="MainWindow" Height="1000" Width="1200">
    <Window.Resources>
        <local:TextLengthToVisibilityConverter x:Key="TextLengthToVisibilityConverter" />
        <!-- Move the Style for ListViewItem outside of ListView -->
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="FontSize" Value="16" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="Killed">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Status}" Value="Opened">
                    <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <!-- TextBox with Watermark -->
        <TextBox x:Name="searchBox" HorizontalAlignment="Center" VerticalAlignment="Top" Width="1100" Height="30" Margin="10,70,0,0" FontSize="16" TextChanged="searchBox_TextChanged">
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="MaxLength" Value="50" />
                    <Setter Property="Foreground" Value="DarkGray" />
                    <Style.Triggers>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
            <TextBox.Template>
                <ControlTemplate TargetType="TextBox">
                    <Grid>
                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <TextBlock x:Name="watermark" Text="Your Watermark Text..." Foreground="LightGray" Margin="5,0,0,0"
                           Visibility="{Binding Text.Length, ElementName=searchBox, Converter={StaticResource TextLengthToVisibilityConverter}}"/>
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Grid>
                </ControlTemplate>
            </TextBox.Template>
        </TextBox>

现在在主窗口构造函数中,我可以添加一个文本,例如:

public MainWindow()
{
    InitializeComponent();

    searchBox.Text = "aaa";
    searchBox.Foreground = Brushes.Black;
}

这将删除水印文本,并添加水印。或者只是在文本框(搜索框)中键入,它将替换水印,当从文本框中删除文本时,水印将再次显示。

wi3ka0sx

wi3ka0sx2#

检查下面的答案:
https://stackoverflow.com/a/6972726/22732851
否则,您也可以设置占位符

<TextBox PlaceholderText="placeholderString"/>

相关问题