C# WPF-如何使用验证规则检查RadioButton上的启用按钮
我刚刚开始学习WPF,对于任何愚蠢的问题,我提前表示歉意。如果所有输入都没有验证错误,我想启用一个按钮。这是我的示例输入窗口:C# WPF-如何使用验证规则检查RadioButton上的启用按钮,c#,wpf,validation,button,radio-button,C#,Wpf,Validation,Button,Radio Button,我刚刚开始学习WPF,对于任何愚蠢的问题,我提前表示歉意。如果所有输入都没有验证错误,我想启用一个按钮。这是我的示例输入窗口: <StackPanel> ...rest of my page <Label>Age:</Label> <TextBox> <TextBox.Text> <Binding Path="Age" UpdateSourceTrigger="P
<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>
…我页面的其余部分
年龄:
根据Stígandr在本主题()中的回答,您只需要在viewModel中创建一个属性,并将Radiobutton IsEnabled/IsChecked属性绑定到此属性
可以在方法中设置该属性
private bool IsValid(DependencyObject obj)
{
return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid);
yourProperty = value;
}
根据Stígandr在本主题()中的回答,您只需要在viewModel中创建一个属性,并将Radiobutton IsEnabled/IsChecked属性绑定到此属性
可以在方法中设置该属性
private bool IsValid(DependencyObject obj)
{
return !Validation.GetHasError(obj) && LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>().All(IsValid);
yourProperty = value;
}
你好,弗里曼,谢谢你的帮助。我不确定这个解决方案是否能解决我的问题。只有在除单选按钮外的任何输入中没有验证错误时,才应启用该按钮。它由CanExecute命令方法控制。您建议创建另一个方法来控制可见性,然后有时由IsValid启用,有时由OnValidationChanged启用?而且,我相信你的意思是使用你的财产=价值;在回来之前,对吗?你好,弗里曼,谢谢你的帮助。我不确定这个解决方案是否能解决我的问题。只有在除单选按钮外的任何输入中没有验证错误时,才应启用该按钮。它由CanExecute命令方法控制。您建议创建另一个方法来控制可见性,然后有时由IsValid启用,有时由OnValidationChanged启用?而且,我相信你的意思是使用你的财产=价值;在返回之前,对吗?
private static readonly DependencyProperty IsFormValidProperty =
DependencyProperty.Register(
"IsFormValid",
typeof(bool),
typeof(YourControl),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnValidationChanged))
);
privatestaticvoid OnValidationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
// here , if e.newValue is true then
//you can set button's IsEnabled property to true.
}