|
| 1 | +using System.Windows; |
| 2 | +using System.Windows.Controls; |
| 3 | + |
| 4 | +namespace Wpf.Ui.Violeta.Controls; |
| 5 | + |
| 6 | +public class BoolStateContentControl : ContentControl |
| 7 | +{ |
| 8 | + public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( |
| 9 | + nameof(Value), |
| 10 | + typeof(bool), |
| 11 | + typeof(BoolStateContentControl), |
| 12 | + new PropertyMetadata(false, OnStateChanged)); |
| 13 | + |
| 14 | + public static readonly DependencyProperty TrueContentProperty = DependencyProperty.Register( |
| 15 | + nameof(TrueContent), |
| 16 | + typeof(object), |
| 17 | + typeof(BoolStateContentControl), |
| 18 | + new PropertyMetadata(null, OnStateChanged)); |
| 19 | + |
| 20 | + public static readonly DependencyProperty FalseContentProperty = DependencyProperty.Register( |
| 21 | + nameof(FalseContent), |
| 22 | + typeof(object), |
| 23 | + typeof(BoolStateContentControl), |
| 24 | + new PropertyMetadata(null, OnStateChanged)); |
| 25 | + |
| 26 | + public bool Value |
| 27 | + { |
| 28 | + get => (bool)GetValue(ValueProperty); |
| 29 | + set => SetValue(ValueProperty, value); |
| 30 | + } |
| 31 | + |
| 32 | + public object? TrueContent |
| 33 | + { |
| 34 | + get => GetValue(TrueContentProperty); |
| 35 | + set => SetValue(TrueContentProperty, value); |
| 36 | + } |
| 37 | + |
| 38 | + public object? FalseContent |
| 39 | + { |
| 40 | + get => GetValue(FalseContentProperty); |
| 41 | + set => SetValue(FalseContentProperty, value); |
| 42 | + } |
| 43 | + |
| 44 | + public BoolStateContentControl() |
| 45 | + { |
| 46 | + UpdateContent(); |
| 47 | + } |
| 48 | + |
| 49 | + private static void OnStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 50 | + { |
| 51 | + if (d is BoolStateContentControl ctrl) |
| 52 | + { |
| 53 | + ctrl.UpdateContent(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void UpdateContent() |
| 58 | + { |
| 59 | + SetCurrentValue(ContentProperty, Value ? TrueContent : FalseContent); |
| 60 | + } |
| 61 | +} |
0 commit comments