Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将事件触发器添加到业务对象的数据模板中?_C#_Wpf_Events_Datatemplate_Eventtrigger - Fatal编程技术网

C# 如何将事件触发器添加到业务对象的数据模板中?

C# 如何将事件触发器添加到业务对象的数据模板中?,c#,wpf,events,datatemplate,eventtrigger,C#,Wpf,Events,Datatemplate,Eventtrigger,我有一个名为BlinkingLight的自定义类。 我也有一个静态的ObservableCollection BlinkingLightCollection。 在UI中,我有一个绑定到BlinkingLightCollection的列表框 在我的列表框中,我希望基本上将每个BlinkingLight对象显示为一个自定义控件,该控件看起来像一个带有LED灯的框,该LED灯有一个动画,使LED看起来像刚刚闪烁了一秒钟,然后恢复正常 我的BlinkingLight类具有第三方“LED”对象,该对象引发

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

在我的列表框中,我希望基本上将每个BlinkingLight对象显示为一个自定义控件,该控件看起来像一个带有LED灯的框,该LED灯有一个动画,使LED看起来像刚刚闪烁了一秒钟,然后恢复正常

我的BlinkingLight类具有第三方“LED”对象,该对象引发一个名为“Flash”的事件

我正在寻找想法或解决方案,使这项工作

我失败的尝试:

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

我为我的列表框创建了一个数据模板:

<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中,而不是使用完全不同的控件,如果这样做可以使事情变得更简单的话

现在我想在我的BlinkingLightControl(或DataTemplate)中有一个EventTrigger,谁的RoutedEvent是LED.Flash事件。不幸的是,我似乎无法理解这一部分。我尝试在我的BlinkingLight类中创建一个RoutedEvent,并在处理LED.Flash事件时将其升高。但是,我的类不是UIElement或ContentElement,根据MSDN:

路由事件所有者可以是任何类,但路由事件必须由UIElement或ContentElement派生类引发并由其处理,才能发挥作用。有关自定义事件的详细信息,请参阅如何:创建自定义路由事件

任何帮助都将不胜感激!! 谢谢
Scott

在这种情况下,最好的方法是使用WPF命令并创建一个“BlinkTheLights”路由命令-您的BlinkingLights控件将处理BlinkTheLights命令,并通过启动一个情节提要来响应,该情节提要执行灯光闪烁

我想出了一个非常有效的解决方案:

因为我的DataTemplate只包含一个自定义UserControl(它绑定到DataContext以从业务对象获取数据)。。。我将自定义RouteEvent放在UserControl中。然后在UserControl的loaded事件中,我将DataContext转换为我的业务对象,以访问包含该事件的业务对象的属性,并将其连接到事件处理程序。(在我的示例中,我将DataContext强制转换为一个BlinkingLight对象,然后我可以访问其Led属性的Flash事件,并将其连接到自定义事件处理程序)。 注意:LED对象必须是属性,而不仅仅是BlinkingLight对象中的字段才能工作

然后事件处理程序可以引发UserControl的自定义路由事件(FlashGreeneEvent)。下面是后端代码,它现在补充了OP中的代码(我已经去掉了任何其他不相关的代码)


如果要创建自定义控件,则始终可以在控件模板之外设置触发器

比如:

  <Style TargetType="{x:Type local:MyControl}">

  <!-- fade in the control with an animation -->
  <Style.Triggers>
    <EventTrigger RoutedEvent="Control.Loaded">
      <BeginStoryboard>
        <Storyboard>
         <DoubleAnimation To="1" Duration="0:0:1" Storyboard.TargetProperty="Opacity"/>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Style.Triggers>

  <!-- Make sure the opacity starts at 0 -->
  <Setter Property="Opacity" Value="0"/>
  <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyControl}">
         </ControlTemplate>
      </Setter.Value>
  </Setter>
</Style>

感谢您的回复!我还没有探索过在WPF中使用命令,但我会尝试一下,让每个人都知道它是如何工作的!
  <Style TargetType="{x:Type local:MyControl}">

  <!-- fade in the control with an animation -->
  <Style.Triggers>
    <EventTrigger RoutedEvent="Control.Loaded">
      <BeginStoryboard>
        <Storyboard>
         <DoubleAnimation To="1" Duration="0:0:1" Storyboard.TargetProperty="Opacity"/>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </Style.Triggers>

  <!-- Make sure the opacity starts at 0 -->
  <Setter Property="Opacity" Value="0"/>
  <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type local:MyControl}">
         </ControlTemplate>
      </Setter.Value>
  </Setter>
</Style>