转换器返回值,但没有任何反应

时间:2018-07-23 13:16:30

标签: c# wpf data-binding ivalueconverter

我正在尝试将自定义文本框的BorderBrush /前景与其文本绑定 在更改文本时调用TextToBrush转换器,并返回正确的Brush,但是实际前景仍然为黑色,并且实际边框完全不可见。 我在做什么错了?

文本框样式:

  <Style TargetType="TextBox" x:Key="ScaleInputStyle">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <Grid >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="5*"/>
                            <RowDefinition Height="5*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="6*"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.Background>
                            <ImageBrush ImageSource="{StaticResource ResourceKey=ScaleBgnd}" />
                        </Grid.Background>

                        <Border x:Name="InputBorder"
                                Grid.Row="1" Grid.Column="1"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="5"
                                BorderBrush="{TemplateBinding BorderBrush}">

                            <TextBox x:Name="Input"
                                     Text="{TemplateBinding Text}"
                                     MaxLength="4"
                                     BorderThickness="0"
                                     BorderBrush="{x:Null}"
                                     Foreground="{TemplateBinding Foreground}"
                                     FontSize="40"
                                     TextAlignment="Center"
                                     HorizontalContentAlignment="Center"
                                     VerticalContentAlignment="Center"
                                     Background="Transparent" />
                        </Border>
                    </Grid>                        
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

文本框:

<TextBox x:Name="ScaleInput"
                         Text="{Binding ElementName=PhotoDisplay, Path=PhotoImage.ScaleSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource TxtBoxToDoubleAndBack}}"
                         Style="{StaticResource ScaleInputStyle}"
                         Grid.Column="3" Grid.Row="4"
                         Foreground="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource TextToBrush}, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
                         Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
                         BorderBrush="{Binding Text, RelativeSource={RelativeSource Self}, Converter={StaticResource TextToBrush}, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" 
                         BorderThickness="3" 
                         IsEnabled="{Binding Path=ActionMode, ElementName=PhotoDisplay, Converter={StaticResource ActionToEnable}, ConverterParameter=EnableOnEvaluate, UpdateSourceTrigger=PropertyChanged}" />

转换器:

public class ConvertTextToBrush : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return Brushes.OrangeRed;
        }

        return Regex.IsMatch(value.ToString(), @"^((\d{1,2})(.\d)?)$", RegexOptions.IgnoreCase | RegexOptions.Singleline)
                   ? Brushes.DodgerBlue
                   : Brushes.OrangeRed;
    }

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

0 个答案:

没有答案