Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. --------------------- Code behind --------------------------
  2. // Using and namespace here .......
  3. public partial class LED1 : UserControl
  4. {
  5. public Brush Txbl1Background
  6. {
  7. get { return (Brush)GetValue(Txbl1BackgroundProperty); }
  8. set { SetValue(Txbl1BackgroundProperty, value); }
  9. }
  10. public static readonly DependencyProperty Txbl1BackgroundProperty =
  11. DependencyProperty.Register("Txbl1Background", typeof(Brush), typeof(LED1), new PropertyMetadata());
  12.  
  13. public Brush Txbl2Background
  14. {
  15. get { return (Brush)GetValue(Txbl2BackgroundProperty); }
  16. set { SetValue(Txbl2BackgroundProperty, value); }
  17. }
  18. public static readonly DependencyProperty Txbl2BackgroundProperty =
  19. DependencyProperty.Register("Txbl2Background", typeof(Brush), typeof(LED1), new PropertyMetadata());
  20.  
  21. public string Text1
  22. {
  23. get { return (string)GetValue(Text1Property); }
  24. set { SetValue(Text1Property, value); }
  25. }
  26. public static readonly DependencyProperty Text1Property =
  27. DependencyProperty.Register("Text1", typeof(string), typeof(LED1), new PropertyMetadata("Text1"));
  28.  
  29. public string Text2
  30. {
  31. get { return (string)GetValue(Text2Property); }
  32. set { SetValue(Text2Property, value); }
  33. }
  34. public static readonly DependencyProperty Text2Property =
  35. DependencyProperty.Register("Text2", typeof(string), typeof(LED1), new PropertyMetadata("Text2"));
  36.  
  37.  
  38. public LED1()
  39. {
  40. InitializeComponent();
  41. }
  42. }
  43. --------------------------------------------------------
  44.  
  45. ------------------------------ XAML ---------------------
  46. <!-- Xaml UserControl Namespace...... />
  47. <StackPanel>
  48. <TextBlock Background="{Binding Txbl1Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Text="{Binding Text1, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
  49. <TextBlock Background="{Binding Txbl2Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Text="{Binding Text2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
  50. </StackPanel>
  51. <!-- Other thing..... />
  52. ---------------------------------------------------------
  53.  
  54. ------------------------ Use ----------------------------
  55. <!-- Caution: namespace will depend on where you put the usercontrol
  56. <local:LED1 Text1="Hello XAML!" Text2="{Binding SomeBinding}" Txbl1Background="Red" Txbl2Background={Binding SomeColorBind}/>
  57.  
  58. or (Set trực tiếp)
  59. myLED1.Txbl1Background = new SolidColorBrush(Color.Red);
  60.  
  61. or (Binding)
  62. Binding bd = new Binding();
  63. bd.Path = new PropertyPath("SomeColorBind");
  64. BindingOperations.SetBinding(myLED1, Txbl2BackgroundProperty, bd);
  65. ----------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement