我有3个单选按钮和一个文本框。 See the image for UI
当用户选择'电话'单选按钮我应该只允许在文本框中输入数字,类似于其他情况的电子邮件和名称。 电子邮件的格式应正确。和名称应以字符开头。 如何在wpf中执行此操作?
答案 0 :(得分:0)
请尝试下一个:
TextBox代码
<TextBox x:Name="TextBoxWithValidation" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding Text, UpdateSourceTrigger=LostFocus, Mode=TwoWay,
NotifyOnValidationError=True, ValidatesOnDataErrors=True}">
<Validation.ErrorTemplate>
<ControlTemplate>
<DockPanel>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder x:Name="ControlWithError"/>
</Border>
<ContentControl x:Name="ValidationSymbol" Margin="-10,0,0,0" Content="{Binding ElementName=ControlWithError,
Path=AdornedElement.(Validation.Errors), Converter={wpfValidationIssueHelpAttempt:Errors2ValidationErrorContentConverter},
UpdateSourceTrigger=PropertyChanged}" ContentTemplate="{StaticResource CommonValidationSymbol}"/>
</DockPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
转换器代码:
public class Errors2ValidationErrorContentConverter : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var errors = value as ReadOnlyCollection<ValidationError>;
ValidationError error;
if (errors == null || (error = errors.FirstOrDefault()) == null) return null;
return error.ErrorContent.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
验证符号代码(将其放入资源部分):
<DataTemplate x:Key="CommonValidationSymbol">
<TextBlock Panel.ZIndex="1000" Foreground="Red" FontFamily="Segoe UI" FontWeight="Bold"
Width="10"
FontSize="15" Background="#0000FF00" Text="!">
<ToolTipService.ToolTip>
<ToolTip x:Name="validationTooltip" Content="{Binding }" Placement="Right"/>
</ToolTipService.ToolTip>
</TextBlock>
</DataTemplate>
这是我的DataContext代码
public class MainViewModel:BaseObservableObject, IDataErrorInfo
{
private string _text;
private string _error;
private bool _isMailChecked;
private bool _isNameChecked;
private bool _isPhoneChecked;
public virtual string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged(() => Text);
}
}
public string this[string columnName]
{
get
{
if (columnName == "Text")
{
if (string.IsNullOrEmpty(Text)) return string.Empty;
Error = string.Empty;
return GetValidationResult();
}
return String.Empty;
}
}
private string GetValidationResult()
{
//define your logic here
if (IsNameChecked)
{
Error = "name is wrong!";
}
if (IsMailChecked)
{
Error = "mail is wrong!";
}
if (IsPhoneChecked)
{
Error = "phone is wrong";
}
return Error;
}
public string Error
{
get { return _error; }
private set
{
_error = value;
OnPropertyChanged(() => Error);
}
}
public bool IsNameChecked
{
get { return _isNameChecked; }
set
{
_isNameChecked = value;
OnPropertyChanged(() => IsNameChecked);
if (value == false) return;
IsMailChecked = false;
IsPhoneChecked = false;
Text = String.Empty;
}
}
public bool IsMailChecked
{
get { return _isMailChecked; }
set
{
_isMailChecked = value;
OnPropertyChanged(() => IsMailChecked);
if (value == false) return;
IsNameChecked = false;
IsPhoneChecked = false;
Text = String.Empty;
}
}
public bool IsPhoneChecked
{
get { return _isPhoneChecked; }
set
{
_isPhoneChecked = value;
OnPropertyChanged(() => IsPhoneChecked);
if (value == false) return;
IsMailChecked = false;
IsNameChecked = false;
Text = String.Empty;
}
}
}
问候。