Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要根据另一个textbox wpf的输入更改textbox的背景吗_C#_Wpf_Textbox_Ivalueconverter - Fatal编程技术网

C# 要根据另一个textbox wpf的输入更改textbox的背景吗

C# 要根据另一个textbox wpf的输入更改textbox的背景吗,c#,wpf,textbox,ivalueconverter,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://sc

我试图根据另一个文本框的输入来更改文本框的背景,就像当我在一个文本框中输入“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>

我正在通过触发器共享一个示例。使用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>

您好,欢迎来到StackOverflow。请共享转换器的代码。
<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();
    }
}