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

时间:2017-04-14 20:36:58

标签: c# wpf xaml mvvm caliburn.micro

我有一个包含另一个FileSystemTree V / VM UserControl的ExplorerWindow 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控件?

提前致谢...

3 个答案:

答案 0 :(得分:0)

嗨hyland Computer Systems,您是否检查过您的视图模型是否真实?在类似的情况下我已经遇到了一个问题,在那里我也将我的视图声明为datacontext(或Ressource,再也没有任何线索)了,当我提出notifypropetychanged或-changing事件时,我没有反应。问题是,我喂了“错误的”vm。我有一个vm,它是由视图创建的,每次注入另一个。注入的那个就是我用过的那个。所以我只是简单地覆盖了我的观点的数据文字,在这里我们去 - &gt;一切正常。

答案 1 :(得分:0)

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

修改 澄清已定义..您的viewmodel中是否有一个具有该名称的方法,该方法将EventArgs作为方法的参数?

运行时,应用输出中是否存在绑定错误?您希望解雇的事件是什么?我敢打赌,我不是正常的投注类型,但我会怀疑绑定错误其次我还怀疑你可能会遇到与视觉树相关的CM的限制

答案 2 :(得分:-2)

使用Galasoft MVVMlight。关于其他视图中包含与否的事件没有此类问题。