我知道有一些类似的线程,但我仍然不确定最好的实现。
代码应该是不言自明的 - 检查那里的评论。如何最好地访问该 VIewModel。
部分:FontSearchBox 是一个没有 ViewModel 的 UserControl - 它只包含一个用于搜索需要执行命令的 TextBox。
感谢和赞赏。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="90" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<part:MainWindowControls Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="/Typesee;component/Resources/window_logo.png" Width="156" Height="45" Grid.Column="0" VerticalAlignment="Top" RenderOptions.BitmapScalingMode="NearestNeighbor" />
<!-- THIS TEXTBOX NEEDS TO CALL A COMMAND (SearchCommand.Execute(string)) Which resides in the fontTreeViewControl's ViewModel (FontTreeViewModel) -->
<part:FontSearchBox Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,12,10" Width="250" DataContext="{Binding ElementName=fontTreeViewControl, Path=DataContext}" />
</Grid>
<vw:FontTreeView x:Name="fontTreeViewControl" Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>