Сценарии - If render 

Page / - Изменено Alexandr Fokin в 2024/08/17 23:34 

If render 
Изменено Alexandr Fokin
в 2024/08/17 23:34

Оглавление

 	Conditional blocks in XAML
https://stackoverflow.com/questions/12999954/conditional-blocks-in-xaml	
how to write an <If> user control for WPF XAML [duplicate]
https://stackoverflow.com/questions/68499748/how-to-write-an-if-user-control-for-wpf-xaml	

 	/// <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>