8

我有两个用于用户名和名的文本框,并且我创建了一个转换器来在文本等于特定字符串时更改文本框的背景颜色。我遇到的问题是文本框只会在运行时更新,并且在我更改文本时不会更新文本框。

XAML:

<TextBox x:Name="forenameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" 
                 Background="{Binding Staff,Converter ={StaticResource StaffNameToBackgroundColourConverter1}}"  
                 Text="{Binding Staff.Forename, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label  Content="Surname:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="2" VerticalAlignment="Center"/>
<TextBox x:Name="surnameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="2"
                 Background="{Binding Staff,Converter={StaticResource StaffNameToBackgroundColourConverter1}}"  
                 Text="{Binding Staff.Surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>

转换器代码:

public class StaffNameToBackgroundColourConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var staff  = (Staff) value;
        if (staff.Forename == "Donald" && staff.Surname == "Duck")
        {
            return "Yellow";
        }
        else
        {
            return "White";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

正确的文本输入:

正确的文字输入

错误的文本输入 - 没有变化:

错误的文本输入 - 没有变化

4

3 回答 3

2

您需要添加UpdateSourceTrigger=PropertyChanged到您的Binding

<TextBox x:Name="forenameTextBox" Grid.Column="1" HorizontalAlignment="Left" 
    Height="23" Margin="3" Grid.Row="1" Background="{Binding Staff, 
    UpdateSourceTrigger=PropertyChanged, Converter ={StaticResource 
    StaffNameToBackgroundColourConverter1}}" Text="{Binding Staff.Forename, 
    Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" 
    VerticalAlignment="Center" Width="120"/>

<TextBox x:Name="surnameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" 
    Margin="3" Grid.Row="2" Background="{Binding Staff, 
    UpdateSourceTrigger=PropertyChanged, Converter={StaticResource 
    StaffNameToBackgroundColourConverter1}}" Text="{Binding Staff.Surname, Mode=TwoWay, 
    NotifyOnValidationError=true, ValidatesOnExceptions=true}" 
    VerticalAlignment="Center" Width="120"/>

这将在用户键入每个字母时更新绑定源。您可以从 MSDN 的Binding.UpdateSourceTrigger 属性页面了解更多信息。

于 2013-09-12T15:19:08.250 回答
2

您应该将一些画笔对象而不是颜色返回到背景,如下所示

public class StaffNameToBackgroundColourConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo 
 culture)
{
    var staff = (Staff)value;
    if (staff.Forename == "Donald" && staff.Surname == "Duck")
    {
        return new SolidColorBrush(Colors.Yellow);

    }
    else
    {
        return new SolidColorBrush(Colors.White);
    }
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    return value;
}
}
于 2014-07-21T02:34:15.327 回答
1

首先,您添加了UpdateSourceTrigger=PropertyChanged错误的绑定。您必须将其添加到Text属性的绑定中。

其次,您将Text属性绑定到Staff.ForenameBackgroundto Staff。当您写入时,该Background属性不知道Staff发生了变化Staff.Forename。当您在属性中写入时,您必须为该属性引发PropertyChanged事件。对.StaffStaff.ForenameStaff.Surname

于 2014-10-22T11:29:39.433 回答