Advertisement
Guest User

Untitled

a guest
Oct 30th, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. Control XAML:
  2. <UserControl x:Class="DPTest.Control" Name="Root">
  3.     <UserControl.DataContext>
  4.         <local:ControlViewModel Test="{Binding MyDependencyProperty, ElementName=Root, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
  5.     </UserControl.DataContext>
  6.     <StackPanel>
  7.         <TextBox Text="{Binding Test, UpdateSourceTrigger=PropertyChanged}" />
  8.         <Button Content="Click me to change ViewModel.Test value to 1234" Command="{Binding ClickCommand}" />
  9.     </StackPanel>
  10. </UserControl>
  11.  
  12. Control code:
  13. namespace DPTest
  14. {
  15.     /// <summary>
  16.     /// Interaction logic for Control.xaml
  17.     /// </summary>
  18.     public partial class Control : UserControl
  19.     {
  20.         public Control()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         public static readonly DependencyProperty MyDependencyPropertyProperty = DependencyProperty.Register(
  26.             "MyDependencyProperty",
  27.             typeof(string),
  28.             typeof(Control),
  29.             new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChanged)
  30.         );
  31.  
  32.         public string MyDependencyProperty
  33.         {
  34.             get { return (string)GetValue(MyDependencyPropertyProperty); }
  35.             set { SetValue(MyDependencyPropertyProperty, value); }
  36.         }
  37.  
  38.         private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  39.         {
  40.             // never fired
  41.         }
  42.     }
  43. }
  44.  
  45. ControlViewModel:
  46.  
  47.     class ControlViewModel : DependencyObject
  48.     {
  49.         public static readonly DependencyProperty TestProperty = DependencyProperty.Register(
  50.             "Test",
  51.             typeof(string),
  52.             typeof(ControlViewModel),
  53.             new FrameworkPropertyMetadata("start value", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPropertyChanged)
  54.         );
  55.  
  56.         public string Test
  57.         {
  58.             get { return (string)GetValue(TestProperty); }
  59.             set { SetValue(TestProperty, value); }
  60.         }
  61.  
  62.         public ICommand ClickCommand { get; set; }
  63.  
  64.         public ControlViewModel()
  65.         {
  66.             ClickCommand = new DelegateCommand(OnClickCommand);
  67.         }
  68.  
  69.         private void OnClickCommand()
  70.         {
  71.             Test = "1234";
  72.         }
  73.  
  74.         private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  75.         {
  76.  
  77.         }
  78.     }
  79.  
  80. Test Window:
  81.  
  82.     <StackPanel>
  83.         <local:Control MyDependencyProperty="{Binding Path=Text, ElementName=CheckDP, UpdateSourceTrigger=PropertyChanged}" />
  84.         <TextBox Name="CheckDP" />
  85.     </StackPanel>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement