首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用textbox绑定数据Wpf

用textbox绑定数据Wpf
EN

Stack Overflow用户
提问于 2017-04-25 06:55:44
回答 3查看 116关注 0票数 0

我有一个像这样的屏幕 图片

输入条件是:

  • 0-4:婴儿(红色)
  • 5-10:儿童(绿色)
  • 11-14:儿童(蓝色)
  • 15-18岁:青少年(黄色)
  • 18-23:成人(器官)

所以当我进口"23“时,它会显示”成人“和”橙色“的颜色!

有人能帮帮我吗?

EN

回答 3

Stack Overflow用户

发布于 2017-04-25 07:13:56

因此,这里将使用两个转换器。

  1. 用于显示名字,如婴儿,成人
  2. 因为他展示了色彩。

触发器在这里不会有帮助,因为触发器检查只等于其他操作符,比如!=<>。为此你必须使用转换器。

这两个转换器将采取您的年龄属性,并将返回价值和颜色的基础上,这样的情况。

票数 1
EN

Stack Overflow用户

发布于 2017-04-25 07:20:07

您可以使用两个转换器,它们将从textbox读取值并返回适当的数据(Text & Color)。

年龄范围到文本转换器:

代码语言:javascript
复制
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();
    }
}

年龄范围到颜色转换器:

代码语言:javascript
复制
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();
    }
}

如下所示,您可以使用转换器:

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2017-04-25 08:09:32

我想出了一个没有转换器的解决方案,因为我发现它们有时有点过火。

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43603677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档