如果出现问题,我想验证文本框。这个想法是,如果出现问题,那么下一个TextBox应该有一个警告图像。
<TextBox Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}">
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel>
<!-- Placeholder for the TextBox itself -->
<AdornedElementPlaceholder x:Name="textBox"/>
<image source="some-Image.png" width="20" Height="20" />
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
但事情是图像没有显示,它只显示图标的边框。
我正确使用AdornedElementPlaceholder
吗?
答案 0 :(得分:0)
经过测试的解决方案,可在发生错误时显示图像:
/**
* Controller action
*/
function myAction() {
if ($this->isAdmin()) {
throw new \Exception('You need to be an admin');
}
$this->userService->create($_POST);
}
/**
* Service containing actions related to users
*/
class UserService {
/**
* @var UserValidator
*/
protected $validator;
/**
* @var RecordSaver
*/
protected $recordSaver;
/**
* @param UserValidator $validator
* @param RecordSaver $recordSaver
*/
public function __construct(
UserValidator $validator,
RecordSaver $recordSaver
) {
$this->validator = $validator;
$this->recordSaver = $recordSaver;
}
/**
* @param $params
* @return array
*/
public function create($params) {
$this->validator->validate($params);
return $this->recordSaver->save($params);
}
}
/**
* Validation of new users / or updating existing
*/
class UserValidator {
/**
* @param array $params
* @throws Exception
*/
public function validate($params) {
if (!isset($params['username'])) {
throw new \Exception('Username must be set');
}
}
}
/**
* Wrapper for database calls
*/
class RecordSaver {
/**
* @param array $params
* @return array
*/
public function save($params) {
return create_record($params);
}
}
以下是我用于此示例的验证规则:
<TextBox BorderThickness="0.8">
<Validation.ErrorTemplate>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder/>
<Image Source="Image.jpg" Width="20" Height="20"/>
</StackPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
<TextBox.Text>
<Binding Path="ValidationTest" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<validation:IntegerValidationRule ValidationStep="CommittedValue" Min="1" Max="99999999"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
这是MSDN Docs的修改示例
有些说明:
您没有提供验证规则,因此我假设它按预期工作并生成有效的验证结果
您的public class IntegerValidationRule : ValidationRule
{
private int _min = int.MinValue;
private int _max = int.MaxValue;
private string _fieldName = "Field";
private string _customMessage = String.Empty;
public int Min
{
get { return _min; }
set { _min = value; }
}
public int Max
{
get { return _max; }
set { _max = value; }
}
public string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
public string CustomMessage
{
get { return _customMessage; }
set { _customMessage = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
int num = 0;
var val = (value as BindingExpression).DataItem;
if (!int.TryParse(value.ToString(), out num))
return new ValidationResult(false, $"{FieldName} must contain an integer value.");
if (num < Min || num > Max)
{
if (!String.IsNullOrEmpty(CustomMessage))
return new ValidationResult(false, CustomMessage);
return new ValidationResult(false, $"{FieldName} must be between {Min} and {Max}.");
}
return new ValidationResult(true, null);
}
}
属性的绑定不包括验证规则。