Исходный код вики If render

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

Последние авторы
1 | |(((
2 |Conditional blocks in XAML
3 [[https:~~/~~/stackoverflow.com/questions/12999954/conditional-blocks-in-xaml>>https://stackoverflow.com/questions/12999954/conditional-blocks-in-xaml]]
4 |how to write an <If> user control for WPF XAML [duplicate]
5 [[https:~~/~~/stackoverflow.com/questions/68499748/how-to-write-an-if-user-control-for-wpf-xaml>>https://stackoverflow.com/questions/68499748/how-to-write-an-if-user-control-for-wpf-xaml]]
6 |
7 )))
8 | |(((
9 |{{code language="c#"}}/// <summary>
10 /// Контрол отображения контента по условию
11 /// </summary>
12 [ContentProperty("ConditionalContent")]
13 public partial class IfControl : UserControl
14 {
15 public static readonly DependencyProperty ConditionProperty
16 = DependencyProperty.Register(
17 "Condition",
18 typeof(bool),
19 typeof(IfControl),
20 new PropertyMetadata(OnConditionChangedCallback)
21 );
22
23 public static readonly DependencyProperty ConditionalContentProperty =
24 DependencyProperty.Register(
25 "ConditionalContent",
26 typeof(object),
27 typeof(IfControl),
28 new PropertyMetadata(null, OnConditionChangedCallback)
29 );
30
31 public bool Condition
32 {
33 get { return (bool)GetValue(ConditionProperty); }
34 set { SetValue(ConditionProperty, value); }
35 }
36 public object ConditionalContent
37 {
38 get { return (object)GetValue(ConditionalContentProperty); }
39 set { SetValue(ConditionalContentProperty, value); }
40 }
41
42 public IfControl()
43 {
44 InitializeComponent();
45 }
46
47 private void OnLoad(object sender, RoutedEventArgs e)
48 {
49 update();
50 }
51
52 private static void OnConditionChangedCallback(
53 DependencyObject sender,
54 DependencyPropertyChangedEventArgs e)
55 {
56 if (sender is IfControl s)
57 {
58 s.update();
59 }
60 }
61
62 private void update()
63 {
64 Content = Condition ? ConditionalContent : null;
65 }
66
67 }{{/code}}|{{code language="xml"}}<tools:IfControl
68 Condition="{Binding IsType1.Data}">
69 <TextBox Text="Type1"></TextBox>
70 </tools:IfControl>{{/code}}
71
72
73 )))