[英]Validate a TextBox upon button press in WPF/MVVM
I have a TextBox
and a Button
in my view. 我的视图中有一个
TextBox
和一个Button
。 I have an ICommand
method, String
serial number property, and a IsEnabled
property in my view model. 我的视图模型中有
ICommand
方法, String
序列号属性和IsEnabled
属性。
When the user clicks the Button
I'd like to validate the serial number in the TextBox
with the InDatabase
property. 当用户单击
Button
我想使用InDatabase
属性验证TextBox
的序列号。 If the contents in the TextBox
are invalid, I would like to raise an error on the TextBox
. 如果
TextBox
中的内容无效,我想在TextBox
上引发错误。 If the contents are valid in the TextBox
I'd like to disable the Button
and execute the command. 如果内容在
TextBox
有效,我想禁用Button
并执行命令。
Here is the view: 这是视图:
<StackPanel Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center">
<UniformGrid Rows="3" >
<TextBlock Text="This device appears to be uninitialized."/>
<UniformGrid Rows="1" Columns="2">
<Label>Serial Number:</Label>
<TextBox Text="{Binding IdentifiedSerialNumber, Mode=TwoWay, ValidatesOnDataErrors=True}"></TextBox>
</UniformGrid>
<Button Content="Identify" Command="{Binding IdentifyCommand}" IsEnabled="{Binding CanExecuteDeviceRestoration}"/>
</UniformGrid>
</StackPanel>
Here is the view-model: 这是视图模型:
public string IdentifiedSerialNumber
{
get
{
return this.identifiedSerialNumber;
}
set
{
this.identifiedSerialNumber = value;
}
}
public ICommand IdentifyCommand
{
get
{
return new RelayCommand(this.RelayRestoreControllerIdentity);
}
}
public bool CanExecuteDeviceRestoration
{
get
{
return canExecuteDeviceRestoration;
}
private set
{
this.canExecuteDeviceRestoration = value;
RaisePropertyChanged("CanExecuteDeviceRestoration");
}
}
public async void RelayRestoreControllerIdentity()
{
await Task.Run(
() =>
{
this.RestoreControllerIdentity();
});
}
public bool InDatebase
{
get
{
return DatabaseConnection.DeviceExists(this.IdentifiedSerialNumber);
}
}
My question is how do I bind the behavior such that when the user click the Button
the TextBox
is validated, and if it fails it displays an error with a message and if it passes the Button
will be disabled and the command will execute. 我的问题是我该如何绑定行为,以便当用户单击
Button
, TextBox
得到验证;如果失败,则显示错误消息,并且如果传递则Button
将被禁用,命令将执行。
You need to implement IDataErrorInfo. 您需要实现IDataErrorInfo。
This would add an indexer which returns a string. 这将添加一个返回字符串的索引器。
If empty string is returned it means no error. 如果返回空字符串,则表示没有错误。
You can return an empty string until button is pressed(u could use a flag). 您可以返回一个空字符串,直到按下按钮为止(您可以使用一个标志)。
when button is pressed,Run the validation logic and appropriately change the flag and Raise PropertyChanged event for IdentifiedSerialNumber 当按下按钮时,运行验证逻辑并适当更改IdentifiedSerialNumber的标志和Raise PropertyChanged事件
You can learn how to implement IDataErrorInfo from here . 您可以从此处了解如何实现IDataErrorInfo。
Also you need to Raise PropertyChanged event for IdentifiedSerialNumber. 另外,您还需要引发IdentifiedSerialNumber的PropertyChanged事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.