想要根据另一个文本框wpf的输入更改文本框的背景

时间:2017-10-25 07:15:39

标签: c# wpf textbox ivalueconverter

我正在尝试根据另一个文本框的输入更改文本框的背景,例如当我将“process”输入到一个文本框中时,另一个文本框的背景应该更改为绿色。 xmal代码是

<Window
    x:Name="mywindow"
    x:Class="labelsetboxreadbox.MainWindow"
        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:labelsetboxreadbox"

        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:StaffNameToBackgroundColourConverter x:Key="converter1"/>
    </Window.Resources>
    <Grid>
        <Label Name="label" Width="150" Height="50" Margin="15,94,352,175" Background="Black"/>
        <TextBox Name="setbox" Width="150" Height="50" Margin="167,95,200,174" Background="{Binding ElementName=mywindow,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource converter1}}" />
        <TextBox Name="readbox" Width="150" Height="50" IsReadOnly="True" Margin="318,95,49,174" Background="Aqua"/>
        <TextBox Name="statusbar" Width="150" Height="50" VerticalAlignment="Bottom" HorizontalAlignment="Right"  TextChanged="statusbar_TextChanged"/>

    </Grid>
</Window>

1 个答案:

答案 0 :(得分:1)

我正在通过触发器共享示例。使用Converter,您也可以使用Element Name属性传递参数。

 <StackPanel>
        <TextBlock Height="20" Name="colorTb" >
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Background" Value="Red"></Setter>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=textTb, Path=Text}" Value="Process">
                            <Setter Property="Background" Value="Green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <TextBox Height="20" Name="textTb" Text="xyz" >

        </TextBox>
    </StackPanel>

这是转换器

<StackPanel>
    <StackPanel.Resources>
        <converters:ColorConverter x:Key="converter1"></converters:ColorConverter>
    </StackPanel.Resources>
    <TextBox Height="20" Name="colorTb" Background="{Binding ElementName=textTb, Path=Text ,Converter={StaticResource converter1}}" >

    </TextBox>
    <TextBox Height="20" Name="textTb" Text="xyz" >

    </TextBox>
</StackPanel>

和转换器代码一样

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var backColor = Brushes.Transparent;
        if (value!=null && value.ToString().Equals("Process"))
        {
            backColor = Brushes.Green;
        }
        return backColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
相关问题