数据绑定Wpf与文本框

时间:2017-04-25 06:55:44

标签: c# wpf

我有一个屏幕,例如pic

输入条件是:

  • 0 - 4:宝贝(红色)
  • 5 - 10:孩子(绿色)
  • 11 - 14:儿童(蓝色)
  • 15 - 18:青少年(黄色)
  • 18 - 23:成年人(organe)

所以当我导入" 23"它将显示"成人"和" Orange"颜色!

是的,有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:1)

因此,本

中将使用2个转换器
  1. 用于显示名称,如Baby,Adult
  2. 显示颜色。
  3. 触发器在此处没有帮助,因为触发器仅检查等于,而不是try { Class<?> c = Class.forName("java.lang.Daemons"); Field maxField = c.getDeclaredField("MAX_FINALIZE_NANOS"); maxField.setAccessible(true); maxField.set(null, Long.MAX_VALUE); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } !=<等其他运营商。为此,你必须使用转换器。

    两个转换器都会使用Age属性,并根据这样的条件返回值和颜色。

答案 1 :(得分:0)

您可以使用两个转换器,它们将从文本框中读取值并返回适当的数据(文本和颜色)。

年龄范围到文字转换器:

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>

答案 2 :(得分:0)

我制作了一个没有转换器的解决方案,因为我发现它们有时候有点矫枉过正

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();
        }
    }
}