多值绑定

代码

  1. <Grid>
  2. <Grid.Resources>
  3. <local:MultipleBoolConverter x:Key="MultipleBoolConverter" />
  4. </Grid.Resources>
  5. <Grid.RowDefinitions>
  6. <RowDefinition Height="*" />
  7. <RowDefinition Height="*" />
  8. <RowDefinition Height="*" />
  9. </Grid.RowDefinitions>
  10. <Grid.ColumnDefinitions>
  11. <ColumnDefinition Width="*" />
  12. <ColumnDefinition Width="*" />
  13. <ColumnDefinition Width="*" />
  14. </Grid.ColumnDefinitions>
  15. <CheckBox
  16. x:Name="chkbox1"
  17. Grid.Row="1"
  18. Grid.Column="1"
  19. HorizontalAlignment="Left"
  20. VerticalAlignment="Top"
  21. Content="条件 1" />
  22. <CheckBox
  23. x:Name="chkbox2"
  24. Grid.Row="1"
  25. Grid.Column="2"
  26. HorizontalAlignment="Left"
  27. VerticalAlignment="Top"
  28. Content="条件 2" />
  29. <CheckBox
  30. Grid.Row="2"
  31. Grid.Column="1"
  32. Content="结果"
  33. IsHitTestVisible="false">
  34. <CheckBox.IsChecked>
  35. <MultiBinding Converter="{StaticResource MultipleBoolConverter}">
  36. <Binding ElementName="chkbox1" Path="IsChecked" />
  37. <Binding ElementName="chkbox2" Path="IsChecked" />
  38. </MultiBinding>
  39. </CheckBox.IsChecked>
  40. </CheckBox>
  41. </Grid>
  1. public class MultipleBoolConverter : IMultiValueConverter
  2. {
  3. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  4. {
  5. var b1 = (bool)values[0];
  6. var b2 = (bool)values[1];
  7. return b1 && b2;
  8. }
  9. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  10. {
  11. throw new NotImplementedException();
  12. }
  13. }

效果

mutiple binding sample.gif