code_junkie

Expose DependencyProperty

Nov 10th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. <UserControl x:Class="WpfApplication3.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  4. <StackPanel Background="LightCyan">
  5. <TextBox Margin="8" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
  6. </StackPanel>
  7. </UserControl>
  8.  
  9.  
  10. using System;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13.  
  14. namespace WpfApplication3
  15. {
  16. public partial class UserControl1 : UserControl
  17. {
  18. public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new PropertyMetadata(null));
  19. public string Text
  20. {
  21. get { return GetValue(TextProperty) as string; }
  22. set { SetValue(TextProperty, value); }
  23. }
  24.  
  25. public UserControl1() { InitializeComponent(); }
  26. }
  27. }
  28.  
  29. <UserControl x:Class="WpfApplication3.UserControl1" x:Name="UserControl1"
  30. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  31. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  32. <StackPanel Background="LightCyan">
  33. <TextBox Margin="8" Text="{Binding Path=Text, ElementName=UserControl1}" />
  34. </StackPanel>
  35. </UserControl>
  36.  
  37. DataContext = this;
  38.  
  39. <TextBox Margin="8" Text="{Binding Text} />
Add Comment
Please, Sign In to add comment