Wpf Prism - 将参数从视图传递到视图模型
Wpf Prism - Passing parameter from view to viewmodel
我创建了一个视图 (MyView),其中包含一个 UserControl,如下所示:
<StackPanel>
<ctrl:ViewDialog DataContext="{Binding CtrlViewDialog}" Message="Hello" Name="ctrlViewDialog" >
</ctrl:ViewDialog>
视图背后的代码:
public MyView()
{
InitializeComponent();
var _message = ctrlViewDialog.Message;
}
[Dependency]
public MyViewViewModel ViewModel
{
get
{
return (MyViewViewModel)this.DataContext;
}
set
{
this.DataContext = value;
}
}
视图模型 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 将这个值从您的视图传递到您的视图模型吗?
是的。它也可以在构造函数中完成,但这需要更多代码。当控件加载更容易时,您可以这样做。
<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; } }
现在这是你应该在棱镜中做的事情吗?传递参数是的。这样做 ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);
和
(MyViewViewModel)this.DataContext;
我创建了一个视图 (MyView),其中包含一个 UserControl,如下所示:
<StackPanel>
<ctrl:ViewDialog DataContext="{Binding CtrlViewDialog}" Message="Hello" Name="ctrlViewDialog" >
</ctrl:ViewDialog>
视图背后的代码:
public MyView()
{
InitializeComponent();
var _message = ctrlViewDialog.Message;
}
[Dependency]
public MyViewViewModel ViewModel
{
get
{
return (MyViewViewModel)this.DataContext;
}
set
{
this.DataContext = value;
}
}
视图模型 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 将这个值从您的视图传递到您的视图模型吗? 是的。它也可以在构造函数中完成,但这需要更多代码。当控件加载更容易时,您可以这样做。
<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; } }
现在这是你应该在棱镜中做的事情吗?传递参数是的。这样做 ViewDialogViewModel CtrlViewDialog = new ViewDialogViewModel(Message);
和
(MyViewViewModel)this.DataContext;