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

时间:2015-07-29 10:50:01

标签: c# wpf xaml eventtrigger

我正在尝试从我的MainWindow 翻转一些矩形。 我想要做的,而不是使用按钮,试图让事件触发翻转。  我尝试将自定义类中的事件链接到 ICommand ,这是众所周知的ScrollBehavior。问题是,当程序加载时,会触发此异常:

Interactivity.TriggersCollection上的XamlParseException

图像类似于:

Rectangles on View

据我所知,事件是

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

这个类与我的MainWindow链接,这要归功于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代码,因为它不相关。

精确度:当我放置一个使用按钮激活Command的代码时,它可以正常工作。 DataEventTrigger替换为:

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

我认为我的活动链接很糟糕。你有什么建议吗? 谢谢你的帮助,我希望我的问题很明确。

1 个答案:

答案 0 :(得分:0)

所以,我所做的就是回答我的需求。我没有使用DataEventTrigger,而是设置DataTrigger

使用INotifyPropertyChanged接口,我可以更改我写的FlipClass中bool的值

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:无法执行操作还有最后一件事需要处理。只有这个代码,滚动是无限的,这个问题将会弹出。必须定义停止触发器。