Wpf 坚持从视图调用视图模型
我有一个树状视图,如下面添加的鼠标双击Wpf 坚持从视图调用视图模型,wpf,prism,Wpf,Prism,我有一个树状视图,如下面添加的鼠标双击 <TreeView Grid.Row="0" Name="tvTopics" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MouseDoubleClick="tvTopics_MouseDoubleClick" ItemsSource="{Binding
<TreeView
Grid.Row="0"
Name="tvTopics"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
MouseDoubleClick="tvTopics_MouseDoubleClick"
ItemsSource="{Binding TierOneItems}"
SelectedItemChanged="treeView1_SelectedItemChanged">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding Topic.IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
<Trigger Property="IsExpanded" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
在这里,我试图将我的“topic”值传递给我的视图模型,但我不知道如何传递或调用我的视图模型方法
public class TopicTreeViewModel : NotificationObject, ITopicTreeViewModel
{
[ImportingConstructor]
public TopicTreeViewModel(IGatewayService storyService, IEventAggregator eventAggregator)
{
this.storyService = storyService;
this.eventAggregator = eventAggregator;
this.AddTopicCommand = new DelegateCommand<object>(this.AddTopic);
Helper.SubscriptionTokenList_LocationSearch.Add(this.eventAggregator.GetEvent<LocationSearchEvent>().Subscribe(OnLocationSearch, ThreadOption.UIThread));
Helper.SubscriptionTokenList_SubjectSearch.Add(this.eventAggregator.GetEvent<SubjectSearchEvent>().Subscribe(OnSubjectSearch, ThreadOption.UIThread));
}
public void MouseDoubleClick(Topic topic)
{
if (topic != null && topic is Topic)
{
switch (this.searchType)
{
case SearchType.Location:
this.eventAggregator.GetEvent<AddLocationEvent>().Publish((Topic)topic);
break;
case SearchType.Subject:
this.eventAggregator.GetEvent<AddSubjectEvent>().Publish((Topic)topic);
break;
}
}
}
公共类TopicTreeViewModel:NotificationObject,ItoPictTreeViewModel
{
[导入构造函数]
public-TopicTreeViewModel(IGatewayService storyService、IEventagor事件聚合器)
{
this.storyService=storyService;
this.eventAggregator=eventAggregator;
this.AddTopicCommand=新的DelegateCommand(this.AddTopic);
添加(this.eventAggregator.GetEvent().Subscribe(OnLocationSearch,ThreadOption.UIThread));
Helper.SubscriptionTokenList_SubjectSearch.Add(this.eventAggregator.GetEvent().Subscribe(OnSubjectSearch,ThreadOption.UIThread));
}
public void MouseDoubleClick(主题)
{
if(topic!=null&&topic为topic)
{
开关(此.searchType)
{
案例搜索类型。位置:
this.eventAggregator.GetEvent().Publish((主题)主题);
打破
案例类型。主题:
this.eventAggregator.GetEvent().Publish((主题)主题);
打破
}
}
}
视图和视图模型之间的接口
public interface ITopicTreeViewModel
{
ReadOnlyCollection<TopicTreeItemViewModel> TierOneItems { get; }
ICommand SearchCommand { get; }
string SearchText { get; set; }
Topic SelectedTopic { get; set; }
}
公共接口iToPictReviewModel
{
只读集合TierOneItems{get;}
ICommand SearchCommand{get;}
字符串搜索文本{get;set;}
主题选择主题{get;set;}
}
当鼠标双击事件触发时,我在这里试图做的就是将主题值传递给我的视图模型
我不知道如何传递或绑定此值。非常感谢任何帮助。特别是在使用Prism和MVVM时,建议尽可能在实现后添加最少的代码。因此,执行的每个逻辑或操作都将直接处理到视图模型中 与其在视图的代码后面处理事件,不如将MouseDoubleClick事件绑定到ViewModel中的委托命令。因此,为了实现这一点,您需要将适当的ViewModel设置为视图的数据上下文g将通过数据上下文实现来解决 以下MSDN Prism指南章节将有助于理解视图和视图模型之间的交互作用:
我希望这能有所帮助。Model.SelectedTopic中的模型是TopicTreeView模型吗?如果是,您可以调用Model.MouseDoubleClickModel.SelectedTopic是我的TopicTreeView模型的界面,用界面代码更新了我的OP
public interface ITopicTreeViewModel
{
ReadOnlyCollection<TopicTreeItemViewModel> TierOneItems { get; }
ICommand SearchCommand { get; }
string SearchText { get; set; }
Topic SelectedTopic { get; set; }
}