简体   繁体   中英

Wpf Prism - Passing parameter from view to viewmodel

I created a view (MyView) where I included a UserControl like this:

<StackPanel>
  <ctrl:ViewDialog DataContext="{Binding CtrlViewDialog}" Message="Hello" Name="ctrlViewDialog" >                     
</ctrl:ViewDialog>

The code behind of the view:

public MyView()
        {
            InitializeComponent();
            var _message = ctrlViewDialog.Message;
        }

        [Dependency]
        public MyViewViewModel ViewModel
        {
            get
            {
                return (MyViewViewModel)this.DataContext;
            }
            set
            {

                this.DataContext = value;
            }
        }

and the view model MyViewViewModel is:

public MyViewViewModel()
        {         

            ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);
        }

The code behind of the included UserControl (ViewDialog) is:

private string message;
        public string Message
        {
            get { return message; }
            set { message = value; }
        }


        public ViewDialog()
        {
            InitializeComponent();
        }

How could I pass the "_message" parameter of MyView to MyViewViewModel in order to pass it to the instance ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);

Ok I will try to answer the tree questions you actually asked. First is related to c#. Can you do this?

public MyViewViewModel()
{      
     ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);
}

No a constructor will always run before you fill properties.

Second can you to pass this value from you View to your view model using WPF? Yes. It could be done in the constructor as well but this would need a lot more code. You can do it when control gets loaded easier.

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:vm="clr-namespace:PrismTest.ViewModels"
             xmlns:view="clr-namespace:PrismTest.Views"
             x:Class="PrismTest.Views.TestView"
             prism:ViewModelLocator.AutoWireViewModel="True">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding Message, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:TestView}}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Message, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type view:TestView}}}"/>
            <TextBlock Text="{Binding Message}"/>
        </StackPanel>
    </Grid>
</UserControl> 

the command

 private ICommand loadedCommand = new DelegateCommand<string>(text => 
        {
            MessageBox.Show(text);
        });
        public ICommand LoadedCommand { get { return loadedCommand; } }

And now is this something you should do in prism? Well passing parameters yes. Doing this ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message); and this

(MyViewViewModel)this.DataContext;

NO!!! If you want to use prism dependency injection is the most important part. You might want to look at this and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM