c# - Wpf Prism - 将参数从 View 传递到 View 模型

标签 c# wpf mvvm prism code-behind

我创建了一个 View (MyView),其中包含一个 UserControl,如下所示:

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

View 背后的代码:

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

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

                this.DataContext = value;
            }
        }

View 模型 MyViewViewModel 是:

public MyViewViewModel()
        {         

            ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);
        }

包含的 UserControl (ViewDialog) 的代码是:

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


        public ViewDialog()
        {
            InitializeComponent();
        }

我如何将 MyView 的 "_message" 参数传递给 MyViewViewModel,以便将其传递给实例 ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);

最佳答案

好的,我将尝试回答您实际提出的树问题。 首先是与c#有关。你能做到吗?

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

没有构造函数会在您填充属性之前始终运行。

其次,您可以使用 WPF 将这个值从您的 View 传递到您的 View 模型吗? 是的。它也可以在构造函数中完成,但这需要更多代码。当控件加载更容易时,您可以这样做。

<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> 

命令

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

现在这是你应该在 Prism 中做的事情吗?传递参数是的。这样做 ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message); 和这个

(MyViewViewModel)this.DataContext;

不!如果你想使用 Prism 依赖注入(inject)是最重要的部分。你可能想看看 thisthis .

关于c# - Wpf Prism - 将参数从 View 传递到 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39535472/

相关文章:

c# - ASP.NET Core 3.0 - InProcess 与 OutOfProcess(HTTP 错误 500.30 - ANCM 进程内启动失败)

c# - Windows Phone - 将 View 绑定(bind)到 View 模型

c# - IDataErrorInfo问题

wpf - 事件 'foo' 不是 RoutedEvent

c# - WPF 复选框检查 IsChecked

mvvm - 在datagrid中添加或删除项目不会触发WhenAnyPropertyChanged

c# - 与 HtmlAgilityPack 中的谓词相关

c# - 数组上的 LINQ 过滤器

c# - 尝试使用错误的 key 解密时 AesCryptoServiceProvider 的行为

wpf - 无法在数据触发器中使用标签的自定义属性