【发布时间】:2017-01-18 05:33:17
【问题描述】:
我刚开始学习 WPF,如有任何愚蠢的问题,请提前道歉。如果所有输入都没有验证错误,我想启用一个按钮。这是我的示例输入窗口:
<StackPanel>
...rest of my page
<Label>Age:</Label>
<TextBox>
<TextBox.Text>
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged"/>
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules
</Binding>
</TextBox.Text>
</TextBox>
<Label>Options:</Label>
<RadioButton>Option one</RadioButton>
<RadioButton>Option two</RadioButton>
<Button Name="btnOK" MinWidth="40" Command="c:CustomCommands.Enter">Ok</Button>
</StackPanel>
在我的后台代码中,按钮的 CanExecute 方法为:
private void EnterCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = isValid(sender as DependencyObject);
}
private bool IsValid(DependencyObject obj)
{
return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid);
}
我在谷歌上看到人们只向一个 RadioButton 添加验证规则,例如在Validation Rule for Radio buttons wpf 中。
我需要的是,当取消选择所有 RadioButton 时,它应该添加一个验证错误,这样按钮就会被 IsValid 方法禁用。如果选择了任何 RadioButton,如果 Age 也为“ok”,则该按钮将被启用。
另外,我需要知道 RadioButton 部分在模型视图类中的情况。
谢谢!
【问题讨论】:
标签: c# wpf validation button radio-button