开始是直接绑定interactivity
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<i:InvokeCommandAction Command="{Binding D_TextChanged_Command}" CommandParameter="{Binding D_RMS}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
由于我的TEXT 也绑定到后台的,这样是ViewModels更新不是实时的,在get set里也不是直接能判断的。
/// <summary>
/// RMS距离D
/// </summary>
public float D
{
get{ return sk.D; }
set
{
sk.D = value;
this.OnPropertyChanged("D");
}
}
<TextBox Name="D_RMS" PreviewTextInput="D_RMS_PreviewTextInput" HorizontalAlignment="Left" Height="36" Margin="750.667,540,0,0" TextWrapping="Wrap" Text="{Binding D, Mode=TwoWay}" VerticalAlignment="Top" Width="164" Grid.Column="1" >
后面直接在.xaml.cs里是
先是
限制输入数字
PreviewTextInput="D_RMS_PreviewTextInput"
//限制输入数字
private void D_RMS_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex re = new Regex("[^0-9]+");
// Regex re = new Regex("[^0-9.-]+");//小数点和负号
e.Handled = re.IsMatch(e.Text);
}
超值提醒, TextChanged="D_RMS_TextChanged" ,TXET改变事件
//输入错误时提醒
private void D_RMS_TextChanged(object sender, TextChangedEventArgs e)
{
D_RMS.ScrollToEnd();
string st = D_RMS.Text;
string hint = fs.Text;
if (st == "" || hint=="")
{
return;
}
int D_vlae = int.Parse(st);
int fs_vlae = int.Parse(hint);
int vlae = 40960 / fs_vlae;
if (D_vlae > vlae)
{
if (co1.IsChecked == true)
{
D_hint.Content = "只能输入比" + vlae.ToString() + "小的整数";
D_hint. Foreground= new SolidColorBrush(Colors.Red);
}
}
else
{
D_hint.Foreground = new SolidColorBrush(Colors.Black);
if (co1.IsChecked == true)
{
D_hint.Content = "s/10";
}
else {
D_hint.Content = "m";
}
}
D_RMS.ScrollToEnd();
}
本来还想在TextChanged写小数点位数限定的,突然想起一个神奇的StringFormat='f2',直接在TEXT,中设置就好了,
<TextBox PreviewTextInput="TextBox_PreviewTextInput" HorizontalAlignment="Left" Height="36" Margin="750.667,300,0,0" TextWrapping="Wrap" Text="{Binding TR0, Mode=TwoWay,StringFormat='f2'}" VerticalAlignment="Top" Width="164" Grid.Column="1"/>
效果