If render

Редактировал(а) Alexandr Fokin 2024/08/17 23:34

 
 
/// <summary>
/// Контрол отображения контента по условию
/// </summary>
[ContentProperty("ConditionalContent")]
public partial class IfControl : UserControl
{
   public static readonly DependencyProperty ConditionProperty
        = DependencyProperty.Register(
           "Condition",
           typeof(bool),
           typeof(IfControl),
           new PropertyMetadata(OnConditionChangedCallback)
            );

   public static readonly DependencyProperty ConditionalContentProperty =
        DependencyProperty.Register(
           "ConditionalContent",
           typeof(object),
           typeof(IfControl),
           new PropertyMetadata(null, OnConditionChangedCallback)
            );

   public bool Condition
    {
       get { return (bool)GetValue(ConditionProperty); }
       set { SetValue(ConditionProperty, value); }
    }
   public object ConditionalContent
    {
       get { return (object)GetValue(ConditionalContentProperty); }
       set { SetValue(ConditionalContentProperty, value); }
    }

   public IfControl()
    {
        InitializeComponent();
    }

   private void OnLoad(object sender, RoutedEventArgs e)
    {
        update();
    }        
   
   private static void OnConditionChangedCallback(
        DependencyObject sender,
        DependencyPropertyChangedEventArgs e)
    {
       if (sender is IfControl s)
        {
            s.update();
        }
    }

   private void update()
    {
        Content = Condition ? ConditionalContent : null;
    }

}
<tools:IfControl
   Condition="{Binding IsType1.Data}">
   <TextBox Text="Type1"></TextBox>
</tools:IfControl>

 

Теги: