Search code examples
c#wpfxamleventtrigger

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


I am trying to flip some rectangles from my MainWindow. What I want to do, instead of using the button, is trying to get an event to fire the flipping. I have tried to link an event in a custom class to a ICommand which is the well-known ScrollBehavior. Problem is, when the program loads, this exception fires up :

XamlParseException on Interactivity.TriggersCollection

The image is something like :

Rectangles on View

From what I have, the event is

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

This very class is linked to my MainWindow thanks to a DataContext as per follow :

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;            

    }

Finally in my XAML code, I have this

 <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>

I have intentionnally cut the rest of the XAML code down, since it is not relevant.

Precision : when I put a code which fires the Command with the button, it works perfectly. The DataEventTrigger is replaced by :

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

I assume my event is badly linked. Do you have any suggestion? Thank you for your help, I hope my question is clear.


Solution

  • SO, what I have done is alittle trick to answer my needs. Instead of using a DataEventTrigger, I set a DataTrigger.

    Using the INotifyPropertyChanged interface, I can change the value of a bool in the FlipClass that I write like this

    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");
               }
           }
       }
    }
    

    The DataContext does not change. Finally, my XML part which contains the trigger will be replaced by this piece of code :

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

    WPF Animation Warning: 6 : Unable to perform action There is still one last thing to take care of. With only this code, the scroll is infinite, and this issue will pop out. A stop trigger has to be defined.