MVVM Light Toolkit EventTrigger绑定不起作用 [英] MVVM Light Toolkit EventTrigger binding doesnt work

查看:108
本文介绍了MVVM Light Toolkit EventTrigger绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WPF项目中实现一个Button,它通过单击并通过释放按钮将变量设置为1。因为我发现无法直接绑定到MouseDown和MouseUp。我试图使用MVVM Light Toolkit,但它无法正常工作。有谁知道处理这个? VS2013 WPF4.5



来自MvvmLightLibs.4.2.30.0的我的DLL:

I am trying to implement a Button in a WPF project, which sets a variable to 1 by clicking and back by releasing the button. Since I found no way to bind to MouseDown and MouseUp directly. I am trying to go with MVVM Light Toolkit, but it is not working. Does anyone know to handle this? VS2013 WPF4.5

My DLLs from MvvmLightLibs.4.2.30.0:

GalaSoft.MvvmLight.Extras.WPF45
GalaSoft.MvvmLight.WPF45
System.Windows.Interactivity





我的XAML:



My XAML:

<UserControl x:Class="PMWA.View.OperatorView"

             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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45">
    <Grid>
                <Button  x:Name="MoveUp"

                         Content="Up"

                         Margin="5">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand Command="{Binding MoveUpCommand}"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="MouseUp">
                            <cmd:EventToCommand Command="{Binding StopMoveCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button x:Name="MoveDown"

                        Content="Down"

                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand Command="{Binding MoveDownCommand}" />
                        </i:EventTrigger>
                        <i:EventTrigger EventName="MouseUp">
                            <cmd:EventToCommand Command="{Binding StopMoveCommand}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button  x:Name="Apply"

                         Content="Apply"

                         Command="{Binding ApplyDistanceCommand}"

                <TextBox x:Name="Distance"

                         Text="{Binding CurrentDistance}"

    </Grid>
</UserControl>





Hier是我的ViewModel:



Hier is my ViewModel:

private ICommand moveUpCommand;
public ICommand MoveUpCommand
{
    get
    {
        if (moveUpCommand == null)
        {
            moveUpCommand = new RelayCommand(p => ExecuteMoveUpCommand());
        }
        return moveUpCommand;
    }
}
private void ExecuteMoveUpCommand()
{
    Trace.WriteLine("Move Up");
    SetPort1(1);
}



绑定在MainWindow.xaml.cs中设置如下:


And the binding is set up in MainWindow.xaml.cs like this:

private void SetupBindings()
{
    var operatorModel = new Model.Operator();
    operatorView.DataContext = new OperatorViewModel(operatorModel, measurementWeightListViewModel);
}



我有第三个按钮,工作正常,但上下按钮不会做任何事情:(


I have i third button which works fine but those up and down buttons wont do anything :(

推荐答案

您好matlab22,



您已经正确编写了代码,但是您试图过度使用它。只需更改按钮代码到这个,让我知道它是怎么回事。

Hi matlab22,

You've written your code correctly, but you are trying to overdo it. Just change the button code to this one and let me know how it goes.
<Button x:name="MoveUp" content="Up" margin="5" command="{Binding MoveDownCommand}" xmlns:x="#unknown" />





希望这会有所帮助,干杯



更新1



所以我理解你的问题不正确(部分原因是你还没有发布所有问题)我需要了解它的代码一目了然。对不起。



你解决问题的方法是正确的,但你对事件的理解不是。这就是为什么你无法使它发挥作用。当事情发生变化时,事件就会发生,而不是只要事情发生。例如,wh你按下按钮上的鼠标左键,它会触发'LeftMouseButtonDown'事件,但只要你拿着它就不会反复触发它。如果您开始单击鼠标左键,它会反复触发它。所以你需要的东西会触发鼠标左键并继续独立于UI线程轮询鼠标左键的状态(显而易见的原因:只要它运行就会阻止UI)。



我的解释已经很久了:D很抱歉,所以这里是代码。我创建了一个新的MVVMLight4.5项目并删除了所有不必要的代码并更新了MainViewModel和MainWindow来实现这一点。



Hope this helps, Cheers

Update 1

So I understood your question incorrectly (partly due to that you haven't posted all the code I needed to understand it at a glance). Sorry for that.

Your approach to the problem is correct, but your understanding of events are not. That's why you haven't been able to make it work. Events occur when something changes, not as long as something is happening. For instance, when you hold down your left mouse button on the button, it fires the 'LeftMouseButtonDown' event but it won't fire it repeatedly, as long as you are holding it. It would fire it repeatedly, if you started clicking your left mouse button. So what you need is something which would trigger on left mouse button down and keep polling it the status of the left mouse button independently of the UI thread (obvious reason: it will block the UI as long as it runs otherwise).

My explanation has got quite long :D Sorry about that, so here goes the code. I created a new MVVMLight4.5 project and removed all the unnecessary code and updated the MainViewModel and MainWindow to achieve this.

public class MainViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            statusData = new StringBuilder();

            worker = new BackgroundWorker {WorkerSupportsCancellation = true};
            worker.DoWork += workerOnDoWork;

            MoveUpCommand = new RelayCommand(moveUpAction);
            StopMoveUpCommand = new RelayCommand(stopMoveUpAction);
        }

        private void workerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            var counter = 0;
            while (!worker.CancellationPending)
            {
                // This is just a guard condition
                if (counter == 100)
                    break;

                Status = "Going up";
                Thread.Sleep(50);
                counter++;
            }

            Status = "Stopped going up";
        }

        private void stopMoveUpAction()
        {
            worker.CancelAsync();
        }

        private void moveUpAction()
        {
            worker.RunWorkerAsync();
        }

        #region Member(s)

        private readonly BackgroundWorker worker;

        private readonly StringBuilder statusData;
        public string Status
        {
            get { return statusData.ToString(); }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                    return;

                statusData.AppendLine(value);
                RaisePropertyChanged("Status");
            }
        }

        public RelayCommand MoveUpCommand { get; private set; }
        public RelayCommand StopMoveUpCommand { get; private set; }

        #endregion
    }



在MainWindow中我将XAML更改为:


And in the MainWindow I changed the XAML to this:

<Grid x:Name="LayoutRoot">
       <Grid.RowDefinitions>
           <RowDefinition Height="Auto" />
           <RowDefinition Height="*" />
       </Grid.RowDefinitions>
       <Button Content="Go up">
           <i:Interaction.Triggers>
               <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                   <command:EventToCommand Command="{Binding MoveUpCommand}"/>
               </i:EventTrigger>
               <i:EventTrigger EventName="PreviewMouseLeftButtonUp">
                   <command:EventToCommand Command="{Binding StopMoveUpCommand}"/>
               </i:EventTrigger>
           </i:Interaction.Triggers>
       </Button>
       <TextBox Text="{Binding Status}" VerticalScrollBarVisibility="Visible" Grid.Row="1" />
   </Grid>



希望这会有所帮助,欢呼


Hope this helps, cheers


我踢了这个mvvm light工具包并且跟着:



Dll:System.Windows。 Interactivity.dll

命名空间:xmlns:i =http://schemas.microsoft.com/expression/2010/intera活动



I kicked this mvvm light toolkit and went with:

Dll: System.Windows.Interactivity.dll
Namespace: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<UserControl x:Class="PMWA.View.OperatorView"

             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:i="http://schemas.microsoft.com/expression/2010/interactivity">
    <Grid>
        <GroupBox Grid.Column="0"

                  Grid.ColumnSpan="2"

                  Grid.RowSpan="5">
            <GroupBox.Header>Operator</GroupBox.Header>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Button  x:Name="MoveUp"

                         Content="Up"

                         Grid.Column="0"

                         Grid.Row="1"

                         Grid.ColumnSpan="2"

                         HorizontalAlignment="Center"

                         VerticalAlignment="Bottom"

                         Width="75"

                         Margin="5">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseDown">
                            <i:InvokeCommandAction Command="{Binding MoveUpCommand}"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="PreviewMouseUp">
                            <i:InvokeCommandAction Command="{Binding StopMoveCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button x:Name="MoveDown"

                        Content="Down"

                        Grid.Column="0"

                        Grid.Row="3"

                        Grid.ColumnSpan="2"

                        HorizontalAlignment="Center"

                        VerticalAlignment="Top"

                        Width="75"

                        Margin="5">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseDown">
                            <i:InvokeCommandAction Command="{Binding MoveDownCommand}" />
                        </i:EventTrigger>
                        <i:EventTrigger EventName="PreviewMouseUp">
                            <i:InvokeCommandAction Command="{Binding StopMoveCommand}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
                <Button  x:Name="Apply"

                         Content="Apply"

                         Command="{Binding ApplyDistanceCommand}"

                         Grid.Column="1"

                         Grid.Row="2"

                         HorizontalAlignment="Left"

                         VerticalAlignment="Center"

                         Width="75"

                         Margin="5" />
                <TextBox x:Name="Distance"

                         Text="{Binding CurrentDistance}"

                         Grid.Column="0"

                         Grid.Row="2"

                         HorizontalAlignment="Left"

                         VerticalAlignment="Center"

                         IsReadOnly="True"

                         TextAlignment="Right"

                         Width="75"

                         Margin="5" />
            </Grid>
        </GroupBox>
    </Grid>
</UserControl>


这篇关于MVVM Light Toolkit EventTrigger绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆