我有一个像这样的屏幕 图片
输入条件是:
所以当我进口"23“时,它会显示”成人“和”橙色“的颜色!
有人能帮帮我吗?
发布于 2017-04-25 07:13:56
因此,这里将使用两个转换器。
触发器在这里不会有帮助,因为触发器检查只等于其他操作符,比如!=、<或>。为此你必须使用转换器。
这两个转换器将采取您的年龄属性,并将返回价值和颜色的基础上,这样的情况。
发布于 2017-04-25 07:20:07
您可以使用两个转换器,它们将从textbox读取值并返回适当的数据(Text & Color)。
年龄范围到文本转换器:
class RangeToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int age = (int)value;
string output = "";
if (age >= 0 && age <= 4)
{
output = "Baby";
}
else if (age >= 5 && age <= 10)
{
output = "Kid";
}
else if (age >= 11 && age <= 14)
{
output = "Children";
}
else if (age >= 15 && age <= 18)
{
output = "Teens";
}
else if (age >= 18 && age <= 23)
{
output = "Adult";
}
return output;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}年龄范围到颜色转换器:
class RangeToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int age = (int)value;
Brush output = Brushes.Transparent;
if (age >= 0 && age <= 4)
{
output = Brushes.Red;
}
else if (age >= 5 && age <= 10)
{
output = Brushes.Green;
}
else if (age >= 11 && age <= 14)
{
output = Brushes.Blue;
}
else if (age >= 15 && age <= 18)
{
output = Brushes.Yellow;
}
else if (age >= 18 && age <= 23)
{
output = Brushes.Orange;
}
return output;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}如下所示,您可以使用转换器:
<Window x:Class="YourNamespace.YourClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespaceWhereYourConvertersAre"
Title="ConverterSample" Height="140" Width="250">
<Window.Resources>
<local:RangeToTextConverter x:Key="RangeToTextConverter " />
<local:ColorToTextConverter x:Key="ColorToTextConverter " />
</Window.Resources>
<StackPanel Margin="10">
<TextBox Name="txtValue" />
<TextBlock Text="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource RangeToTextConverter}}" />
<Rectangle Fill="{Binding ElementName=txtValue, Path=Text, Converter={StaticResource ColorToTextConverter}}" />
</StackPanel>
</Window>发布于 2017-04-25 08:09:32
我想出了一个没有转换器的解决方案,因为我发现它们有时有点过火。
public class YourClass : INotifyPropertyChanged
{
private int _num;
public int Num
{
get => _num;
set
{
if (Equals(value, _num)) return;
_num = value;
if (value >= 0 && value <= 4)
{
Col = Brushes.Red;
}
else if (value >= 5 && value <= 10)
{
Col = Brushes.Green;
}
else if (value >= 11 && value <= 14)
{
Col = Brushes.Blue;
}
else if (value >= 15 && value <= 18)
{
Col = Brushes.Yellow;
}
else if (value >= 18 && value <= 23)
{
Col = Brushes.Orange;
}
OnPropertyChanged();
}
}
private Brush _col;
public Brush Col
{
get => _col;
set {
_col = value;
OnPropertyChanged();
}
}
}https://stackoverflow.com/questions/43603677
复制相似问题