Как напечатать textBlock, используя условные выражения в XAML? - PullRequest
2 голосов
/ 12 августа 2011

Мне нужно напечатать с повторением textBlock, если число меньше 80 и закрасить красным цветом, а печать более или равна 80 успешна с зеленым цветом.

Как это сделать в XAML?

Ответы [ 2 ]

3 голосов
/ 12 августа 2011

Преобразователи .

К сожалению, не существует триггеров неравенства и т. П., Поэтому следует использовать преобразователь.

<TextBlock>
    <TextBlock.Foreground>
        <Binding Path="TestDouble">
            <Binding.Converter>
                <vc:ThresholdConverter BelowValue="{x:Static Brushes.Red}"
                                       AboveValue="{x:Static Brushes.Green}"
                                       Threshold="80" />
            </Binding.Converter>
        </Binding>
    </TextBlock.Foreground>
    <TextBlock.Text>
        <Binding Path="TestDouble">
            <Binding.Converter>
                <vc:ThresholdConverter BelowValue="Repeat"
                                       AboveValue="Successful"
                                       Threshold="80" />
            </Binding.Converter>
        </Binding>
    </TextBlock.Text>
</TextBlock>
public class ThresholdConverter : IValueConverter
{
    public double Threshold { get; set; }

    public object AboveValue { get; set; }
    public object BelowValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double input;
        if (value is double)
        {
            input = (double)value;
        }
        else
        {
            var converter = new DoubleConverter();
            input = (double)converter.ConvertFrom(value);
        }
        return input < Threshold ? BelowValue : AboveValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
2 голосов
/ 12 августа 2011
<local:NumberToBrushConverter x:Key="numberToBrushConverter" />
<local:NumberToTextConverter x:Key="numberToTextConverter" />

<TextBlock Background="{Binding Number, Converter={StaticResource numberToBrushConverter}}"                    
Text="{Binding Number, Converter={StaticResource numberToTextConverter}"/>

class NumberToBrushConverter: IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int number = (int)value;

        return number < 80 ? Brushes.Red : Brushes.Green;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    #endregion
}

Другой конвертер будет выглядеть аналогично конвертеру кистей, но выдает «Successful» или «Repeat».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...