Caliburn Micro 中包含的 ViewModel 事件未触发

Contained ViewModel Events in Caliburn Micro Not Firing

我有一个 ExplorerWindow V/VM UserControl 包含另一个 FileSystemTree V/VM UserControl。

MainVM 维护的 Screens 集合中都不包含这些控件,因为 ExplorerWindowView 显示为模式弹出窗口。

事件正在 ExplorerWindowView 上触发;但是,尚不清楚附加事件如何或为何未在包含的 FileSystemView 上触发。

ExplorerWindowView

<UserControl x:Class="KTronInd.Wpf.ControlLibrary.FileExplorer.Views.ExplorerWindowView"
         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:view="clr-namespace:KTronInd.Wpf.ControlLibrary.FileExplorer.Views"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         x:Name="Explorer"
         xmlns:cal="http://www.caliburnproject.org"
         cal:Message.Attach="[Event Loaded]=[Action ExplorerLoaded($eventArgs)]"
         mc:Ignorable="d" 
         d:DesignHeight="300"
         d:DesignWidth="500">


<Grid x:Name="OuterGrid" 
      Width="800" 
      Height="400">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition  />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="5" />
    </Grid.RowDefinitions>

    <!--Display the DirectoryTree on the left side of the grid DataContext="{Binding Explorer, Source={StaticResource Locator}}" -->
    <view:FileSystemTreeView Grid.Column="0"
                             DataContext="{Binding FileTreeVM}"
                             Grid.Row="0"  
                             Visibility="Visible"/>

    <!-- pane splitter-->
    <GridSplitter Grid.Column="0" 
                  Grid.Row="0" 
                  HorizontalAlignment="Right" 
                  Width="3" 
                  Background="Gray" 
                  Visibility="Visible" />

    <!--Display the File and Directory Selector on the right side of the grid DataContext="{Binding Explorer, Source={StaticResource Locator}}" -->
    <ScrollViewer Grid.Column="1" Grid.Row="0" >
        <view:DirectoryViewerView />
    </ScrollViewer>

</Grid>

...和 ​​ExplorerWindowViewModel

[Export( typeof( IScreen ) ), PartCreationPolicy( CreationPolicy.NonShared )]
public class ExplorerWindowViewModel : Screen, IHandle<ModelEvent>
{
[ImportingConstructor]
public ExplorerWindowViewModel( IEventAggregator events )
{
    m_Events = events;
    FileTreeVM = new FileSystemTreeViewModel( this , m_Events );
    //FileTreeVM.ConductWith( this ); 
.
.
}
.
.
public FileSystemTreeViewModel FileTreeVM
    {
        get
        {
            return _fileTreeVM;
        }
        set
        {
            _fileTreeVM = value;
            NotifyOfPropertyChange( () => FileTreeVM );
        }
    }   
}

这是 FileSystemTreeView

UserControl x:Class="KTronInd.Wpf.ControlLibrary.FileExplorer.Views.FileSystemTreeView"
         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:cal="http://www.caliburnproject.org"
         xmlns:vm="clr-namespace:KTronInd.Wpf.ControlLibrary.FileExplorer.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" >

<UserControl.Resources>

    <vm:GetFileSysemInformationConverter x:Key="getFileSysemInformationConverter"/>

    <HierarchicalDataTemplate DataType = "{ x:Type vm:DirInfo }" 
                              ItemsSource = "{ Binding Converter={ StaticResource getFileSysemInformationConverter } }" >

        <StackPanel Orientation="Horizontal">

            <Image Width="20" 
                   Height="20" 
                   Stretch="Fill" 
                   x:Name="img" />

            <TextBlock Text="{ Binding Name }" 
                       Margin="5,0,0,0" />

        </StackPanel>

        <!-- The HDT triggers bind the directory to an image representing the object type. -->
        <HierarchicalDataTemplate.Triggers>
            <!-- My Computer -->
            <DataTrigger Binding="{ Binding Path = DirType }" 
                         Value="0">

                <Setter Property="Image.Source" 
                        TargetName="img" 
                        Value="..\Images\MyComputer.jpg" />

            </DataTrigger>

            <!-- A disk drive -->
            <DataTrigger Binding="{ Binding Path = DirType }" 
                         Value="1">

                <Setter Property="Image.Source" 
                        TargetName="img" 
                        Value="..\images\diskdrive.png" />

            </DataTrigger>

            <!-- A folder -->
            <DataTrigger Binding="{ Binding Path = DirType }" 
                         Value="2">

                <Setter Property="Image.Source" 
                        TargetName="img"
                        Value="..\images\folder.png" />

            </DataTrigger>

        </HierarchicalDataTemplate.Triggers>

    </HierarchicalDataTemplate>

    <!-- TreeViewItem property setters -->
    <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>

</UserControl.Resources>

<Grid Background="Beige" >

    <TreeView x:Name="DirectoryTree"  
              ItemsSource="{Binding Path = FileTreeVM.SystemDirectorySource}"
              cal:Message.Attach="[Event Loaded]=[Action FSTViewLoaded($eventArgs)]"
              BorderThickness="0"
              VerticalAlignment="Stretch"
              HorizontalAlignment="Stretch" 
              Width="300" >

    </TreeView>
</Grid>

最后,FileSystemTreeViewModel

[Export( typeof( IScreen ) ), PartCreationPolicy( CreationPolicy.NonShared )]
public class FileSystemTreeViewModel : Screen , IHandle<ModelEvent>
{
    [ImportingConstructor]
    public FileSystemTreeViewModel( ExplorerWindowViewModel evm , IEventAggregator events )
    {
        m_Events = events;
        InitVM( evm );
    }    
}

如何让事件在 ExplorerWindowView 中包含的 FileSystemTreeView 或 FileSystemTreeView 内的 TreeView 控件中触发?

提前致谢...

您好 hyland Computer Systems,您是否检查过您的视图模型是否是真实的?在类似的情况下,我已经遇到了一个问题,我还将我的 vm 声明为数据上下文(或 Ressource,不再有任何线索),并且在我提出 notifypropetychanged 或 -changing 事件时没有任何反应。问题是,我喂 "wrong" 虚拟机。我有一个虚拟机,它是由视图创建的,另一个是每次注入创建的。注射的就是那个,我喂的。所以我只是再次覆盖了我的视图的数据上下文,然后我们开始 -> 一切正常。

cal:Message.Attach="[Event Loaded]=[Action FSTViewLoaded($eventArgs)]"你真的定义了这个动作吗?

编辑: 澄清定义.. 你的视图模型中是否有一个名称为 EventArgs 作为方法参数的方法?

您在应用程序运行时的输出中是否存在绑定错误?您期望触发什么事件?我会打赌,我通常不是博彩类型,但我会怀疑绑定错误其次我也会怀疑你可能会击中与视觉树相关的 CM 的限制

使用 Galasoft MVVMlight。关于位于其他视图中的事件,无论是否包含,都没有此类问题。