ButtonGroup.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Controls.Primitives;
  4. using System.Windows.Media;
  5. using HandyControl.Data;
  6. namespace HandyControl.Controls;
  7. public class ButtonGroup : ItemsControl
  8. {
  9. protected override bool IsItemItsOwnContainerOverride(object item) => item is Button or RadioButton or ToggleButton;
  10. public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
  11. nameof(Orientation), typeof(Orientation), typeof(ButtonGroup), new PropertyMetadata(default(Orientation)));
  12. public Orientation Orientation
  13. {
  14. get => (Orientation) GetValue(OrientationProperty);
  15. set => SetValue(OrientationProperty, value);
  16. }
  17. public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register(
  18. nameof(Layout), typeof(LinearLayout), typeof(ButtonGroup), new PropertyMetadata(LinearLayout.Uniform));
  19. public LinearLayout Layout
  20. {
  21. get => (LinearLayout) GetValue(LayoutProperty);
  22. set => SetValue(LayoutProperty, value);
  23. }
  24. protected override void OnRender(DrawingContext drawingContext)
  25. {
  26. var count = Items.Count;
  27. for (var i = 0; i < count; i++)
  28. {
  29. var item = (ButtonBase) Items[i];
  30. item.Style = ItemContainerStyleSelector?.SelectStyle(item, this);
  31. }
  32. }
  33. }