瞄到BindingGroup用法
文章转载于https://www.cnblogs.com/dangnianxiaoqingxin/p/12653988.html
2.BindingGroup的使用
public class MyClass { public int StartValue { get; set; } public int EndValue { get; set; } }
public class RangeValidationRule : ValidationRule { public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bindingGroup = (BindingGroup)value; MyClass mc = (MyClass)bindingGroup.Items[0]; int startValue = (int)bindingGroup.GetValue(mc, "StartValue"); int endValue = (int)bindingGroup.GetValue(mc, "EndValue"); bool isEnabled = (bool)bindingGroup.GetValue(mc, "IsEnabled"); if (startValue > endValue) { return new ValidationResult(true, null); } return new ValidationResult(false, "Input should between 0 and 100"); } }
MyClass mc = new MyClass(); public MainWindow() { InitializeComponent(); this.DataContext = mc; }
<Grid Name="Grid1" Width="400" Height="300" > <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.BindingGroup> <BindingGroup NotifyOnValidationError="True"> <BindingGroup.ValidationRules > <local:RangeValidationRule2 ValidationStep="ConvertedProposedValue" /> </BindingGroup.ValidationRules> </BindingGroup> </Grid.BindingGroup> <TextBox Grid.Row="0" Name="tb1" Text="{Binding StartValue}"> </TextBox> <TextBox Grid.Row="1" Text="{Binding EndValue}"/> <Button Name="btn1" Content="btn1" Grid.Row="2" Click="Button_Click" > </Button> </Grid>
验证:
this.Grid1.BindingGroup.CommitEdit();
文章转载于https://www.cnblogs.com/dangnianxiaoqingxin/p/12653988.html
转载保留源出处即可,商业使用请自行鉴别,使用本博客中公开内容做任何违法犯罪于本作者无关