-1

私はいくつかの同様のスレッドがあることを知っていますが、私はまだ最良の実装がわかりません。

コードは自明である必要があります-そこでコメントを確認してください。そのVIewModelにアクセスするための最良の方法。

part: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では、 TextBox Xaml で書く<vw:FontTreeView />必要があるので、持っている必要がありますTextBox

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 に答える