从WPF中的第一个控件执行第二个控件的方法

时间:2019-05-26 09:38:45

标签: wpf mvvm user-controls relaycommand routed-commands

我的ViewControl有一个称为ZoomIn()的方法。如何通过单击ViewControl控件在此Button上执行此方法,而无需进行代码隐藏。

<controls:ViewControl/>
<Button Content="Zoom In"/>

ViewControl.xaml.cs:

    public void ZoomIn()
    {
        double actualWidth = m_child.ActualWidth;
        double actualHeight = m_child.ActualHeight;

        double x = (0.5 * actualWidth - Dx) / Scale;
        double y = (0.5 * actualHeight - Dy) / Scale;

        float startScale = Scale;

        Scale = Math.Min(Scale * ZoomFactor, ZoomMax);

        Dx = (float)x * (startScale - Scale) + Dx;
        Dy = (float)y * (startScale - Scale) + Dy;
    }

尽管我有一个ViewModel并试图在设计中使用MVVM。我不确定在这种情况下怎么可能,因为ZoomIn()做的是与视图相关的事情。

我能想到的一种类似情况是,当我有一个Button和一个TextBox时,我想在单击Button时在TextBox上调用SelectAll()方法。

1 个答案:

答案 0 :(得分:1)

实际上有几种不同的方法可以执行此操作,一种解决方案是使用行为和包装器类将事件绑定到事件。首先为您的视图模型将触发的事件定义一个包装器:

public class EventTriggerWrapper
{
    public event EventHandler OnTriggered;

    public void Trigger()
    {
        this.OnTriggered?.Invoke(this, EventArgs.Empty);
    }
}

出于演示的目的,这里是按钮和WebBrowser的一些XAML,我将在视图模型中使用包装器的实例,以在每次按下按钮时触发Web浏览器的Navigate()函数:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Button Content="Click Me" Command="{Binding NavigateCommand}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" />        

    <WebBrowser Grid.Row="1">
        <i:Interaction.Behaviors>
            <behaviors:MyCustomBehavior EventTrigger="{Binding EventTrigger}" />
        </i:Interaction.Behaviors>
    </WebBrowser>

</Grid>

您可以看到我已经向Web浏览器控件添加了自定义行为,并且该行为已绑定到名为EventTrigger的视图模型属性。您需要将其与按钮的命令处理程序一起添加到视图模型中:

public class MainViewModel
{
    public EventTriggerWrapper EventTrigger { get; } = new EventTriggerWrapper();

    private ICommand _NavigateCommand;
    public ICommand NavigateCommand => this._NavigateCommand ?? (this._NavigateCommand = new RelayCommand(OnNavigate));

    private void OnNavigate()
    {
        this.EventTrigger.Trigger();
    }
}

剩下的所有事情就是使用预订事件的属性创建行为,然后在目标控件中调用所需的任何函数:

public class MyCustomBehavior : Behavior<WebBrowser>
{
    public EventTriggerWrapper EventTrigger
    {
        get { return (EventTriggerWrapper)GetValue(EventTriggerProperty); }
        set { SetValue(EventTriggerProperty, value); }
    }

    // Using a DependencyProperty as the backing store for EventTrigger.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EventTriggerProperty =
        DependencyProperty.Register("EventTrigger", typeof(EventTriggerWrapper), typeof(MyCustomBehavior), new PropertyMetadata(null, OnEventTriggerChanged));

    private static void OnEventTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var behaviour = d as MyCustomBehavior;
        var oldValue = e.OldValue as EventTriggerWrapper;
        if (oldValue != null)
            oldValue.OnTriggered -= behaviour.OnEventTriggered;
        var newValue = e.NewValue as EventTriggerWrapper;
        if (newValue != null)
            newValue.OnTriggered += behaviour.OnEventTriggered;
    }

    private void OnEventTriggered(object sender, EventArgs e)
    {
        if (this.AssociatedObject != null)
            this.AssociatedObject.Navigate("http://www.google.com");    // <-- change this to the function you want to invoke
    }
}