c# – ContentControl中的GroupBox – 支持由绑定到ContentControl的内容实现的IDataErrorInfo

前端之家收集整理的这篇文章主要介绍了c# – ContentControl中的GroupBox – 支持由绑定到ContentControl的内容实现的IDataErrorInfo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个viewmodel,它代表多个选项并实现IDataErrorInfo.此viewmodel仅在选择了至少其中一个选项时才有效.它绑定到ContentControl. DataTemplate用于将viewmodel可视化为包含ItemsControl的GroupBox.另一个DataTemplate将每个选项可视化为CheckBox.

我需要做什么,使ContentControl与IDataErrorInfo一起工作,并在选中或取消选中复选框时检查有效性?

一些代码

捆绑:

  1. <ContentControl Content="{Binding GeneralInvoiceTypes,ValidatesOnDataErrors=True}"
  2. Margin="0,5,0" />

数据模板:

  1. <DataTemplate DataType="{x:Type viewmodels:MultipleOptionsviewmodel}">
  2. <GroupBox Header="{Binding Title}">
  3. <ItemsControl ItemsSource="{Binding Options}" />
  4. </GroupBox>
  5. </DataTemplate>
  6. <DataTemplate DataType="{x:Type viewmodels:Optionviewmodel}">
  7. <CheckBox IsChecked="{Binding IsChecked}"
  8. Content="{Binding Name}"
  9. Margin="6,3,0" />
  10. </DataTemplate>

样式:

  1. <Style TargetType="{x:Type ContentControl}">
  2. <Style.Triggers>
  3. <Trigger Property="Validation.HasError"
  4. Value="true">
  5. <Setter Property="ToolTip"
  6. Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}" />
  7. </Trigger>
  8. </Style.Triggers>
  9. <Setter Property="Validation.ErrorTemplate">
  10. <Setter.Value>
  11. <ControlTemplate>
  12. <Grid>
  13. <Grid.ColumnDefinitions>
  14. <ColumnDefinition Width="90*" />
  15. <ColumnDefinition Width="20" />
  16. </Grid.ColumnDefinitions>
  17. <Border BorderBrush="Red"
  18. BorderThickness="1"
  19. CornerRadius="2.75"
  20. Grid.Column="0">
  21. <AdornedElementPlaceholder Grid.Column="0" />
  22. </Border>
  23. <TextBlock Foreground="Red"
  24. Grid.Column="1"
  25. Margin="0"
  26. FontSize="12"
  27. VerticalAlignment="Center"
  28. HorizontalAlignment="Left"
  29. x:Name="txtError">
  30. *
  31. </TextBlock>
  32. </Grid>
  33. </ControlTemplate>
  34. </Setter.Value>
  35. </Setter>
  36. </Style>

解决方法

What do I have to do,to make the ContentControl work together with
IDataErrorInfo and check the validity when a check Box is checked or
unchecked?

添加一点Rachels回答.

使用异步数据验证更容易解决此问题,但遗憾的是,这在WPF 4.5 is released之前不可用.

内容绑定到Mainviewmodel中的GeneralInvoiceTypes.由于我们不能进行异步数据验证,因此必须为GeneralInvoiceTypes引发PropertyChanged以进行验证.这可行,但我会采用Rachel建议的方法,并在MultipleOptionsviewmodel中引入另一个名为IsValid的属性

可以从Tag(或附加属性)到GeneralInvoiceTypes.IsValid完成对IsValid的绑定.当在任何选项中更改IsChecked时,我们还必须在MultipleOptionsviewmodel中得到通知.例如,可以通过在CheckBoxes中使用命令绑定来完成此操作.

因此,需要沿着以下几行进行一些更改.

我还上传了一个示例项目,在此实现:https://www.dropbox.com/s/fn8e4n4s68wj3vk/ContentControlValidationTest.zip?dl=0

ContentControl中

  1. <ContentControl Content="{Binding Path=GeneralInvoiceTypes}"
  2. Tag="{Binding Path=GeneralInvoiceTypes.IsValid,ValidatesOnDataErrors=True}" />

Optionviewmodel DataTemplate

  1. <DataTemplate DataType="{x:Type viewmodels:Optionviewmodel}">
  2. <CheckBox IsChecked="{Binding IsChecked}"
  3. Content="{Binding Name}"
  4. Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContentControl}},Path=DataContext.IsValidCheckCommand}"
  5. Margin="6,0" />
  6. </DataTemplate>

MultipleOptionsviewmodel

  1. private ICommand m_isValidCheckCommand;
  2. public ICommand IsValidCheckCommand
  3. {
  4. get
  5. {
  6. return m_isValidCheckCommand ??
  7. (m_isValidCheckCommand = new RelayCommand(param => IsValidCheck()));
  8. }
  9. }
  10.  
  11. private void IsValidCheck()
  12. {
  13. IsValid = CheckIsValid();
  14. }
  15.  
  16. private bool CheckIsValid()
  17. {
  18. foreach (Optionviewmodel option in Options)
  19. {
  20. if (option.IsChecked == true)
  21. {
  22. return true;
  23. }
  24. }
  25. return false;
  26. }
  27.  
  28. private bool m_isValid;
  29. public bool IsValid
  30. {
  31. get { return m_isValid; }
  32. set
  33. {
  34. m_isValid = value;
  35. OnPropertyChanged("IsValid");
  36. }
  37. }
  38.  
  39. public string this[string columnName]
  40. {
  41. get
  42. {
  43. if (columnName == "IsValid")
  44. {
  45. if (IsValid == false)
  46. {
  47. return "At least 1 Option must be selected";
  48. }
  49. }
  50. return string.Empty;
  51. }
  52. }

猜你在找的C#相关文章