C#/WPF - DataEventTrigger 或如何触发事件

C#/WPF - DataEventTrigger or how to trigger an Event

我正在尝试从我的主窗口翻转一些矩形。 我想做的不是使用按钮,而是尝试获取一个事件来触发翻转。 我尝试将自定义 class 中的 event link 转换为众所周知的 ScrollBehavior ICommand。问题是,当程序加载时,会触发此异常:

Interactivity.TriggersCollection

上的 XamlParseException

图片是这样的:

据我所知,事件是

public class FlipClass
{ 
    int i = 0;
    public void FlipEvent(object sender, EventArgs e)
    {
        i++;
        Console.WriteLine(i);
        myTestingString = johnny;
    }
}

这 class 被 link 编辑到我的主窗口,这要归功于 DataContext,如下所示:

public MainWindow()
    {
        InitializeComponent();

        FlipClass() _flipClass = new FlipClass();



        this.WindowStartupLocation = WindowStartupLocation.CenterScreen;


        DataContext = _flipClass;       //The Context is here


        Stopwatch moi = new Stopwatch();
        moi.Start(): //Monitoring the Window



        moi.Stop();
        Console.WriteLine(moi.ElapsedMilliseconds);


        Closing += OnClosing;            

    }

最后在我的 XAML 代码中,我有这个

 <ec:PathListBox x:Name="pathListBox" Margin="312,566.254,299.5,407" WrapItems="True" ItemContainerStyle="{DynamicResource PathListBoxItemStyle1}">
        <i:Interaction.Behaviors>
            <local:PathListBoxScrollBehavior>
                <i:Interaction.Triggers>
                    <local:DataEventTrigger EventName="FlipEvent">    <------- This is Where I get the Exception
                        <i:InvokeCommandAction CommandName="DecrementCommand"/>
                    </local:DataEventTrigger>
                </i:Interaction.Triggers>
                <local:PathListBoxScrollBehavior.Ease>
                    <QuarticEase EasingMode="EaseOut"/>
                </local:PathListBoxScrollBehavior.Ease>
            </local:PathListBoxScrollBehavior>
        </i:Interaction.Behaviors>
        <ec:PathListBox.LayoutPaths>
            <ec:LayoutPath SourceElement="{Binding ElementName=path}" Distribution="Even" Capacity="3"/>
        </ec:PathListBox.LayoutPaths>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
        <Rectangle HorizontalAlignment="Left" Height="139.96" Stroke="#FF6DBDD1" StrokeThickness="1" VerticalAlignment="Top" Width="202" Fill="#FFA7CEF5"/>
    </ec:PathListBox>

我有意删减了 XAML 代码的其余部分,因为它不相关。

Precision :当我放置一个用按钮触发命令的代码时,它工作得很好。 DataEventTrigger 替换为:

<i:EventTrigger SourceName="button" EventName="Click">
                        <i:InvokeCommandAction CommandName="DecrementCommand"/>
                    </i:EventTrigger>

我认为我的活动 link 很糟糕。你有什么建议吗? 谢谢你的帮助,我希望我的问题很清楚。

所以,我所做的是一个小技巧来满足我的需求。我没有使用 DataEventTrigger,而是设置了 DataTrigger

使用 INotifyPropertyChanged 接口,我可以更改我这样编写的 FlipClass 中的布尔值

public class FlipClass
{
private bool myBool = false;
public void FlipEvent(object sender, EventArgs e)
{
     myBool = true;
}

#region INotifyPropertyChanged implementation 


   public event PropertyChangedEventHandler PropertyChanged; 

   public void OnPropertyChanged(PropertyChangedEventArgs e)
   {


       PropertyChangedEventHandler h = PropertyChanged;
       if (h != null)
       {
           h(this, e);
       }
       else
       {
           Console.WriteLine("fail");
       }
   }

   public void OnPropertyChanged(string propertyName)
   {
       OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
   }

   #endregion INotifyPropertyChanged implementation


   public bool CompareMyBool {
       get { return myBool;}
       set
       {
           if (value != myBool)
           {
               myBool = value;
               OnPropertyChanged("CompareMyBool");
           }
       }
   }
}

DataContext 没有改变。最后,我的包含触发器的 XML 部分将被这段代码替换:

<ei:DataTrigger Binding="{Binding CompareMyBool}" Value="True">
                        <i:InvokeCommandAction CommandName="IncrementCommand"/>
                    </ei:DataTrigger>

WPF 动画警告:6:无法执行操作 还有最后一件事需要处理。只有这段代码,滚动是无限的,就会弹出这个问题。必须定义停止触发器。