-1

我知道有一些类似的线程,但我仍然不确定最好的实现。

代码应该是不言自明的 - 检查那里的评论。如何最好地访问该 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>
4

1 回答 1

1

如果您使用 MVVMLight,您可以使用 Event to Command 来...

在您的 UserControl 中<vw:FontTreeView />,您必须TextBox在 TextBox Xaml 中有一个所以您必须编写

Xaml

<i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
        <Commands:EventToCommand Command="{Binding Path=TextChangedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

要了解有关别名的更多信息,请参阅链接。它还演示了如何将事件参数传递给您的 ViewModel.... 但您可能不会这样做,因此您可以跳过它...

如果 TextBox 是外部 fontTreeViewControl.... 那么,

<StackPanel DataContext={Binding Path=DataContext,ElementName=fontTreeViewControl}>
    <TextBox >
    <i:Interaction.Triggers>
    <i:EventTrigger EventName="TextChanged">
        <Commands:EventToCommand Command="{Binding Path=TextChangedCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
    </TextBox>
</StackPanel>

这可能会帮助你.... :)

于 2011-11-25T11:07:59.073 回答