Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <UserControl x:Name="userControl" x:Class="Test_Paramaterized_UserControl_with_MVVM.UserControl1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:Test_Paramaterized_UserControl_with_MVVM"
- xmlns:view="clr-namespace:Daavlin.SmartTouch.STUV_WPF.View"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="300">
- <Grid Margin="10">
- <Border BorderThickness="3" BorderBrush="Black" Padding="10">
- <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="UserControl1 View: "/>
- <TextBlock Text="{Binding ElementName=userControl, Path=PropUserControlView, Mode=OneWay}" FontWeight="Bold"/>
- </StackPanel>
- <Rectangle Height="5"/>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="UserControl1 ViewModel: " />
- <TextBlock Text="{Binding PropUserControlViewModel, FallbackValue=propUserControlViewModel 2}" FontWeight="Bold">
- <TextBlock.DataContext>
- <local:UserControl1ViewModel/>
- </TextBlock.DataContext>
- </TextBlock>
- </StackPanel>
- </StackPanel>
- </Border>
- </Grid>
- </UserControl>
- public partial class UserControl1 : UserControl
- {
- public UserControl1()
- {
- InitializeComponent();
- }
- public string PropUserControlView { get => (string)GetValue(PropUserControlViewProperty); set => SetValue(PropUserControlViewProperty, value); }
- public static readonly DependencyProperty PropUserControlViewProperty =
- DependencyProperty.Register(nameof(PropUserControlView), typeof(string), typeof(UserControl1),
- new PropertyMetadata(null, DependencyPropertyChanged));
- private static void DependencyPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
- {
- var x = dependencyPropertyChangedEventArgs.NewValue;
- }
- }
- public class UserControl1ViewModel : ObservableObject
- {
- public string PropUserControlViewModel { get => _propUserControlViewModel; set => SetField(ref _propUserControlViewModel, value); }
- private string _propUserControlViewModel = "value from UserControl-ViewModel";
- }
- <Window x:Class="Test_Paramaterized_UserControl_with_MVVM.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:Test_Paramaterized_UserControl_with_MVVM"
- Title="MainWindow" >
- <Window.DataContext>
- <local:MainWindowViewModel />
- </Window.DataContext>
- <Grid VerticalAlignment="Top" >
- <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="20">
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="MainWindow1 ViewModel: "/>
- <TextBox Text="{Binding PropWindowViewModel, UpdateSourceTrigger=PropertyChanged}" FontWeight="Bold"/>
- </StackPanel>
- <Rectangle Height="10"/>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="UserControl1 (fixed value Fixed): " VerticalAlignment="Center"/>
- <local:UserControl1 PropUserControlView="Fixed"/>
- </StackPanel>
- <!--<Rectangle Height="10"/>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="UserControl1 (bound to MainWindows VM via binding Path): " VerticalAlignment="Center"/>
- <local:UserControl1 PropUserControlView="{Binding ElementName=MainWindow1, Path=DataContext.PropWindowViewModel}"/>
- </StackPanel>-->
- <Rectangle Height="10"/>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="UserControl1 (bound to MainWindows VM): " VerticalAlignment="Center"/>
- <local:UserControl1 PropUserControlView="{Binding PropWindowViewModel}"/>
- </StackPanel>
- </StackPanel>
- </Grid>
- </Window>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- }
- public class MainWindowViewModel : ObservableObject
- {
- public string PropWindowViewModel { get => _propWindowViewModel; set => SetField(ref _propWindowViewModel, value); }
- private string _propWindowViewModel = "valuefrom Window-VIewModel";
- }
Add Comment
Please, Sign In to add comment