Blazor中的InputNumber绑定转换器

问题描述 投票:0回答:1

我需要使用一些转换器来修改赋予InputNumber组件的数值。有谁知道如何像使用WPF值转换器一样在绑定过程中修改值?例如,在绑定属性中将给定值除以10(用户输入10 =>属性设置为1,但对用户仍显示为10)?

如果我只想显示百分比(模型属性0.57 =>显示值57%,怎么办?我可以使用某种格式来实现此目的吗?

blazor converters
1个回答
0
投票
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit"> <DataAnnotationsValidator /> <ValidationSummary /> <InputNumber id="name" @bind-Value="exampleModel.PropertyAsInt" /> <InputNumber @bind-Value="exampleModel.PropertyAsDouble" /> <button type="submit">Submit</button> </EditForm> @code { private ExampleModel exampleModel = new ExampleModel(); private void HandleValidSubmit() { Console.WriteLine(exampleModel.GetFormatedIntPorperty); Console.WriteLine($"{exampleModel.GetFormatedDouble}%"); } }

模型:

public class ExampleModel
    {
        public int PropertyAsInt { get; set; } = 10;
        public double PropertyAsDouble { get; set; } = 57;

        public int GetFormatedIntPorperty
        {
            get
            {
                return int.Parse(PropertyAsInt.ToString()[0..1]);
            }
        }

        public double GetFormatedDouble
        {
            get
            {
                return PropertyAsDouble / 100;
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.