你如何添加一个事件触发器为业务对象数据模板? [英] How do you add an Event Trigger to a data template for a business object?

查看:262
本文介绍了你如何添加一个事件触发器为业务对象数据模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为BlinkingLight的自定义类。
我也有一个静态的ObservableCollection BlinkingLightCollection。
在UI,我有一个绑定到BlinkingLightCollection一个列表框。

I have a custom class named BlinkingLight. I also have a static ObservableCollection BlinkingLightCollection. In the UI, I have a ListBox that is bound to BlinkingLightCollection.

在我的列表框我想基本上是显示每个BlinkingLight对象,看起来自定义控件就像有一个动画,使LED看起来像是刚刚在一秒钟,然后恢复到正常的一个LED灯框。

In my ListBox I want to essentially display each BlinkingLight object as a custom control that looks like box with an LED light that has an animation that makes the LED look like it just flashed on for a second then goes back to normal.

我BlinkingLight类有第三方LED对象,提出了所谓的'闪光'的事件。

My BlinkingLight class has third party "LED" object that raises an event called 'Flash'.

我要寻找的想法或解决方案来得到这个工作!

I am looking for ideas or solutions to get this to work!

我的失败尝试:

我创建了一个自定义的控制(BlinkingLightControl),可以绑定到我的BlinkingLight类的数据当一个BlinkingLight是我的自定义控件的DataContext的

I created a custom control (BlinkingLightControl) that can bind to the data of my BlinkingLight class when a BlinkingLight is the DataContext of my custom control.

我创建了一个DataTemplate我的列表框:

I created a DataTemplate for my ListBox:

<Window.Resources>
  <DataTemplate x:Key="blinkingLightItemTemplate" >
    <local:BlinkingLightControl />
  </DataTemplate>
</Window.Resources>

<ListBox ItemsSource={Binding Source={x:Static local:Data.BlinkingLightCollection}}
         ItemTemplate="{StaticResource blinkingLightItemTemplate}" />

请注意:我只是把XAML为我的自定义控件到的DataTemplate而不是一个完全不同的控制如果让事情变得更容易。

Note: I can just put the xaml for my custom control into the datatemplate instead having a completely different control if that makes things easier.

现在我想有一个EventTrigger在我BlinkingLightControl(或DataTemplate中)谁是RoutedEvent是LED.Flash事件。不幸的是,我似乎无法推测这部分了。我试着在我BlinkingLight类来创建RoutedEvent,只是提高它,每当我处理LED.Flash事件。不过我的课不是的UIElement或ContentElement的,每MSDN: MSND链接

Now I want to have an EventTrigger in my BlinkingLightControl (or DataTemplate) who's RoutedEvent is the LED.Flash event. Unfortunately I can't seem to figure this part out. I've tried to create a RoutedEvent in my BlinkingLight class and just raise it whenever I handle the LED.Flash event. However my class is not a UIElement or ContentElement, and per MSDN: MSND Link

路由事件所有者可以是任何类,但是路由事件必须得到提高,并以有用通过的UIElement或ContentElement的派生类处理。欲了解更多信息有关自定义事件,请参阅如何创建自定义路由事件

"The routed event owner can be any class, but routed events must be raised by and handled by UIElement or ContentElement derived classes in order to be useful. For more information about custom events, see How to: Create a Custom Routed Event."

任何帮助将不胜感激!!
谢谢,
斯科特

Any help would be greatly appreciated!! Thanks, Scott

推荐答案

我能够拿出已经做得很不错的解决方案

I was able to come up with a solution that has worked quite well:

由于我的DataTemplate只包含一个自定义的用户控件(结合到DataContext从业务对象获取其数据)......我把我的自定义RoutedEvent在该用户。然后在我的用户控件的加载事件,我投在DataContext为我的业务对象以访问业务对象的属性,有事件,并把它挂到事件处理程序。 (在我的例子中,我投在DataContext作为BlinkingLight对象,那么我可以访问其LED财产的Flash事件,并把它挂到一个自定义事件处理程序)。
注:LED对象必须是一个属性,而不是只是在BlinkingLight对象为它工作现场

Since my DataTemplate simply contains a custom UserControl (which binds to the DataContext to get its data from the business object)... I placed my custom RoutedEvent in the UserControl. Then in my UserControl's loaded event, I cast the DataContext as my business object to get access to the business object's property that has the event and hook it up to an event handler. (in my example I cast the DataContext as a BlinkingLight object, then I can get access to its Led property's Flash event and hook it up to a custom event handler). Note: The LED object must be a property, not just a field in the BlinkingLight object for it to work.

然后,事件处理程序可以提高用户控件的自定义的路由事件(FlashGreenEvent)。下面是后端代码,现在补充了OP代码(我已经去掉了任何其他不相关的代码)。

Then the event handler can raise the UserControl's custom Routed Event (FlashGreenEvent). Below is the back end code that now supplements the code in the OP (I've stripped out any other irrelevant code).

public partial class BlinkingLightControl : UserControl
{
    public static readonly RoutedEvent FlashGreenEvent = EventManager.RegisterRoutedEvent("FlashGreen", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(BlinkingLightControl));
    public event RoutedEventHandler FlashGreen
    {
        add { AddHandler(FlashGreenEvent, value); }
        remove { RemoveHandler(FlashGreenEvent, value); }
    }

    private void BlinkingLightControl_Loaded(object sender, RoutedEventArgs e)
    {
        BlinkingLight blinkingLight = (BlinkingLight)this.DataContext;

        blinkingLight.Led.Flash += LED_Flash;
    }

    protected delegate void LED_FlashCallback(ThirdParty.LED sender);
    public void LED_Flash(ThirdParty.LED sender)
    {
        if (this.Dispatcher.CheckAccess())
        {
            // Raise the Flash Green Event;
            RaiseEvent(new RoutedEventArgs(BlinkingLightControl.FlashGreenEvent));
        }
        else
            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new LED_FlashCallback(LED_Flash), sender);
    }
}

这篇关于你如何添加一个事件触发器为业务对象数据模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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