Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------- Code behind --------------------------
- // Using and namespace here .......
- public partial class LED1 : UserControl
- {
- public Brush Txbl1Background
- {
- get { return (Brush)GetValue(Txbl1BackgroundProperty); }
- set { SetValue(Txbl1BackgroundProperty, value); }
- }
- public static readonly DependencyProperty Txbl1BackgroundProperty =
- DependencyProperty.Register("Txbl1Background", typeof(Brush), typeof(LED1), new PropertyMetadata());
- public Brush Txbl2Background
- {
- get { return (Brush)GetValue(Txbl2BackgroundProperty); }
- set { SetValue(Txbl2BackgroundProperty, value); }
- }
- public static readonly DependencyProperty Txbl2BackgroundProperty =
- DependencyProperty.Register("Txbl2Background", typeof(Brush), typeof(LED1), new PropertyMetadata());
- public string Text1
- {
- get { return (string)GetValue(Text1Property); }
- set { SetValue(Text1Property, value); }
- }
- public static readonly DependencyProperty Text1Property =
- DependencyProperty.Register("Text1", typeof(string), typeof(LED1), new PropertyMetadata("Text1"));
- public string Text2
- {
- get { return (string)GetValue(Text2Property); }
- set { SetValue(Text2Property, value); }
- }
- public static readonly DependencyProperty Text2Property =
- DependencyProperty.Register("Text2", typeof(string), typeof(LED1), new PropertyMetadata("Text2"));
- public LED1()
- {
- InitializeComponent();
- }
- }
- --------------------------------------------------------
- ------------------------------ XAML ---------------------
- <!-- Xaml UserControl Namespace...... />
- <StackPanel>
- <TextBlock Background="{Binding Txbl1Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Text="{Binding Text1, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
- <TextBlock Background="{Binding Txbl2Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" Text="{Binding Text2, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
- </StackPanel>
- <!-- Other thing..... />
- ---------------------------------------------------------
- ------------------------ Use ----------------------------
- <!-- Caution: namespace will depend on where you put the usercontrol
- <local:LED1 Text1="Hello XAML!" Text2="{Binding SomeBinding}" Txbl1Background="Red" Txbl2Background={Binding SomeColorBind}/>
- or (Set trực tiếp)
- myLED1.Txbl1Background = new SolidColorBrush(Color.Red);
- or (Binding)
- Binding bd = new Binding();
- bd.Path = new PropertyPath("SomeColorBind");
- BindingOperations.SetBinding(myLED1, Txbl2BackgroundProperty, bd);
- ----------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement