我有一个类型为import React from "react";
import Highcharts from "highcharts";
import HC_exporting from "highcharts/modules/exporting";
import HC_exporting_data from "highcharts/modules/export-data";
import PropTypes from "prop-types";
class HCLineChart extends React.Component {
constructor(props){
super(props);
HC_exporting(Highcharts);
HC_exporting_data(Highcharts);
}
componentDidMount() {
this.highChartsRender(this.props.options);
}
shouldComponentUpdate(nextProps) {
this.highChartsRender(nextProps.options);
return false;
}
highChartsRender(options) {
Highcharts.chart(this.props.id,{
chart: {
type: "line",
renderTo: "lineChart",
height: 500,
},
title: {
text: this.props.title,
},
xAxis: {
categories: options.xAxisCategories,
},
yAxis: {
title: {
text: this.props.title,
},
},
plotOptions: {
series: {
marker: {
enabled: false,
},
},
},
credits:{
enabled: false,
},
exporting: {
buttons: {
contextButton: {
enabled: true,
menuItems: [
"printChart",
"downloadCSV",
"downloadXLS",
],
},
},
},
series: options.data,
});
Highcharts.setOptions({
lang: {
thousandsSep: ",",
},
});
}
render() {
return <div className="line-chart" id={this.props.id} />;
}
}
export default HCLineChart;
的控件,我不想让用户输入以“ 0”开头的值,例如...
0
01
00
00010
但是如果我想输入自然数形式的包含“ 0”的值,例如...
10
2010
200000
<Entry>
:
MyView.XAML
<Entry
HorizontalOptions="FillAndExpand"
Placeholder="Cantidad"
Keyboard="Numeric"
MaxLength="9"
Text="{Binding CantidadContenedor}"></Entry>
:
MyViewModel.CS
捕获 string cantidadContenedor;
public string CantidadContenedor
{
get
{
return cantidadContenedor;
}
set
{
if (cantidadContenedor != value)
{
cantidadContenedor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CantidadContenedor)));
}
}
}
的值后如何添加此验证?
我可以占用Entry
类型为TryParse
的{{1}}属性吗?
对我有帮助吗?
答案 0 :(得分:3)
这个工作完美!
public MainPage()
{
InitializeComponent();
MyEntry.TextChanged+= MyEntry_TextChanged;
}
void MyEntry_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
{
if(!string.IsNullOrWhiteSpace(MyEntry.Text))
{
if (MyEntry.Text.StartsWith("0"))
MyEntry.Text = e.OldTextValue;
}
}
答案 1 :(得分:1)
您可以转换任何以string
的价格接收的值。
然后只需将第一个数字作为子字符串读取,如下所示:
using System;
public class Program
{
public static void Main()
{
string x = "0TEST";
if(x.StartsWith("0")){
Console.WriteLine("Starts with 0");
}else{
Console.WriteLine("Doesn't start with 0");
}
}
}
然后写逻辑以允许/拒绝它。
答案 2 :(得分:1)
我将创建一个验证规则。
在您的XAML中
<Entry
HorizontalOptions="FillAndExpand"
Placeholder="Cantidad"
Keyboard="Numeric"
MaxLength="9">
<Entry.Text>
<Binding Path=CantidadContenedor>
<Binding.ValidationRules>
<validationRules:StartWithRule Prefix="0"/>
</Binding>
</Entry.Text>
</Entry>
现在,您可以创建自己认为合适的验证了。就您而言
public class StartWithRule : ValidationRule
{
public string Prefix { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
// Pass the value to string
var numberToCheck = (string) value;
if (numberToCheck == null) return ValidationResult.ValidResult;
// Check if it starts with prefix
return numberToCheck.StartsWith(Prefix)
? new ValidationResult(false, $"Starts with {Prefix}")
: ValidationResult.ValidResult;
}
}
您应该很好。