【发布时间】:2017-04-14 20:36:57
【问题描述】:
我有一个 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 );
}
}
}
这是文件系统树视图
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 控件?
提前谢谢...
【问题讨论】:
标签: c# wpf xaml mvvm caliburn.micro