Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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# 如何在XAML中使用条件打印文本块?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何在XAML中使用条件打印文本块?

C# 如何在XAML中使用条件打印文本块?,c#,wpf,xaml,C#,Wpf,Xaml,我需要在文本块中重复打印,如果数字小于80并将颜色涂成红色,而大于或等于80则使用绿色打印成功 如何在XAML中实现这一点? 不幸的是,不存在不平等触发器之类的东西,所以使用转换器就可以了 <TextBlock> <TextBlock.Foreground> <Binding Path="TestDouble"> <Binding.Converter> <vc:T

我需要在文本块中重复打印,如果数字小于80并将颜色涂成红色,而大于或等于80则使用绿色打印成功

如何在XAML中实现这一点?

不幸的是,不存在不平等触发器之类的东西,所以使用转换器就可以了

<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>

公共类阈值转换器:IValueConverter
{
公共双阈值{get;set;}
公共对象值{get;set;}
公共对象低于值{get;set;}
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
双输入;
如果(值为双精度)
{
输入=(双)值;
}
其他的
{
var转换器=新的双转换器();
输入=(双)转换器。ConvertFrom(值);
}
返回输入<阈值?低于值:高于值;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotSupportedException();
}
}


类别编号BrushConverter:IValueConverter
{
#区域转换器成员
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
整数=(整数)值;
返回编号<80?刷子。红色:刷子。绿色;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
不做任何事;
}
#端区
}

另一个转换器看起来与笔刷转换器类似,但返回“成功”或“重复”。

但是使用转换器,如何将这些值设置为属性?@oscar.imbres:您需要将其绑定到与文本相同的值。(上面是一个示例实现,您也可以直接绑定到
文本
,但是转换器需要首先解析输入值),如果您不知道如何使用数据绑定,请参阅.@oscar.imbres:将其编辑为使用通用双转换器,现在您还可以将其绑定到文本。我明白了,还有一个问题:是否可以同时设置文本?我的意思是:@oscar.imbres:这只是转换器的创建,与文本无关<代码>文本和前景是独立设置的。
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();
    }
}
<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
}