Guest User

Untitled

a guest
Jan 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. <UserControl x:Name="userControl" x:Class="Test_Paramaterized_UserControl_with_MVVM.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. xmlns:local="clr-namespace:Test_Paramaterized_UserControl_with_MVVM"
  7. xmlns:view="clr-namespace:Daavlin.SmartTouch.STUV_WPF.View"
  8. mc:Ignorable="d"
  9. d:DesignHeight="300" d:DesignWidth="300">
  10. <Grid Margin="10">
  11. <Border BorderThickness="3" BorderBrush="Black" Padding="10">
  12. <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
  13. <StackPanel Orientation="Horizontal">
  14. <TextBlock Text="UserControl1 View: "/>
  15. <TextBlock Text="{Binding ElementName=userControl, Path=PropUserControlView, Mode=OneWay}" FontWeight="Bold"/>
  16. </StackPanel>
  17. <Rectangle Height="5"/>
  18. <StackPanel Orientation="Horizontal">
  19. <TextBlock Text="UserControl1 ViewModel: " />
  20. <TextBlock Text="{Binding PropUserControlViewModel, FallbackValue=propUserControlViewModel 2}" FontWeight="Bold">
  21. <TextBlock.DataContext>
  22. <local:UserControl1ViewModel/>
  23. </TextBlock.DataContext>
  24. </TextBlock>
  25. </StackPanel>
  26. </StackPanel>
  27. </Border>
  28. </Grid>
  29. </UserControl>
  30.  
  31. public partial class UserControl1 : UserControl
  32. {
  33. public UserControl1()
  34. {
  35. InitializeComponent();
  36. }
  37.  
  38. public string PropUserControlView { get => (string)GetValue(PropUserControlViewProperty); set => SetValue(PropUserControlViewProperty, value); }
  39. public static readonly DependencyProperty PropUserControlViewProperty =
  40. DependencyProperty.Register(nameof(PropUserControlView), typeof(string), typeof(UserControl1),
  41. new PropertyMetadata(null, DependencyPropertyChanged));
  42.  
  43. private static void DependencyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
  44. {
  45. var x = dependencyPropertyChangedEventArgs.NewValue;
  46. }
  47. }
  48.  
  49. public class UserControl1ViewModel : ObservableObject
  50. {
  51. public string PropUserControlViewModel { get => _propUserControlViewModel; set => SetField(ref _propUserControlViewModel, value); }
  52. private string _propUserControlViewModel = "value from UserControl-ViewModel";
  53. }
  54.  
  55. <Window x:Class="Test_Paramaterized_UserControl_with_MVVM.MainWindow"
  56. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  57. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  58. xmlns:local="clr-namespace:Test_Paramaterized_UserControl_with_MVVM"
  59. Title="MainWindow" >
  60. <Window.DataContext>
  61. <local:MainWindowViewModel />
  62. </Window.DataContext>
  63. <Grid VerticalAlignment="Top" >
  64. <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="20">
  65. <StackPanel Orientation="Horizontal">
  66. <TextBlock Text="MainWindow1 ViewModel: "/>
  67. <TextBox Text="{Binding PropWindowViewModel, UpdateSourceTrigger=PropertyChanged}" FontWeight="Bold"/>
  68. </StackPanel>
  69. <Rectangle Height="10"/>
  70. <StackPanel Orientation="Horizontal">
  71. <TextBlock Text="UserControl1 (fixed value Fixed): " VerticalAlignment="Center"/>
  72. <local:UserControl1 PropUserControlView="Fixed"/>
  73. </StackPanel>
  74. <!--<Rectangle Height="10"/>
  75. <StackPanel Orientation="Horizontal">
  76. <TextBlock Text="UserControl1 (bound to MainWindows VM via binding Path): " VerticalAlignment="Center"/>
  77. <local:UserControl1 PropUserControlView="{Binding ElementName=MainWindow1, Path=DataContext.PropWindowViewModel}"/>
  78. </StackPanel>-->
  79. <Rectangle Height="10"/>
  80. <StackPanel Orientation="Horizontal">
  81. <TextBlock Text="UserControl1 (bound to MainWindows VM): " VerticalAlignment="Center"/>
  82. <local:UserControl1 PropUserControlView="{Binding PropWindowViewModel}"/>
  83. </StackPanel>
  84. </StackPanel>
  85. </Grid>
  86. </Window>
  87.  
  88. public partial class MainWindow : Window
  89. {
  90. public MainWindow()
  91. {
  92. InitializeComponent();
  93. }
  94. }
  95.  
  96. public class MainWindowViewModel : ObservableObject
  97. {
  98. public string PropWindowViewModel { get => _propWindowViewModel; set => SetField(ref _propWindowViewModel, value); }
  99. private string _propWindowViewModel = "valuefrom Window-VIewModel";
  100. }
Add Comment
Please, Sign In to add comment