激发订阅特定事件处理程序的所有事件

本文关键字:程序 事件 事件处理 | 更新日期: 2023-09-27 18:28:37

我有一个列表视图

 <ListView Name="listView2">
        <ListView.View>
            <GridView>
                <!-- First Column -->
                <GridViewColumn Width="105" Header="ID">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>              
                            <StackPanel>    
                                      <!-- here is the event xID_Loaded -->    
                                <TextBox Loaded="xID_Loaded" Text="{Binding ID}"></TextBox>
                                <Popup Height="Auto"  Width="100" IsOpen="True" >
                                    <ListView Margin="2">
                                    </ListView>
                                </Popup>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <!-- Second Column -->
                <GridViewColumn Width="185" Header="Description">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Description}" ></TextBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
               etc...

无论如何,listview都绑定到一个可观察的集合。每次向列表视图添加新行时,我都会使用列上的Loaded="xID_Loaded"事件初始化弹出控件。

因此,在我身后的代码中,我有:

private void xID_Loaded(object sender, RoutedEventArgs e)
{
    // sender is the textbox
    // I can get the popup relative to the sender
    var stackPanel = (StackPanel)(((Control)sender).Parent);
    var popup = return (Popup)sp.Children[1];
    // every time window moves I want to reset the location
    // of the popu by doing:
    this.LocationChanged += (a, b) =>
    {
        popup.IsOpen = false;   // this will ensure that the popup moves with the control
        popup.IsOpen = true;
    };
   // some more code to initialice the popup
   // ...

现在我想要一个名为ResetAllPopups()的方法,该方法将重置所有弹出窗口。以防有人滚动或出于任何原因,我想重置所有教皇。

我知道我将能够通过以下方式解决这个问题:

使用VisualTreeHelper遍历listview中的listviewitems,找到弹出控件,然后重置找到的每个弹出控件。

我想如果我能找到所有订阅的活动会更好。LocationChanged事件处理程序。我如何才能执行所有订阅到这个的事件。LocationChanged事件就像我在哪里更改窗口的位置一样

激发订阅特定事件处理程序的所有事件

您认为过于以UI为中心,只需将IsOpen绑定到您的项,然后对其进行迭代,无需获得Popup实例。

    public event EventHandler reset_Popus;
    private void xID_Loaded(object sender, RoutedEventArgs e)
    {
        // I can get the popup relative to the sender
       var stackPanel = (StackPanel)(((Control)sender).Parent);
       var popup = return (Popup)sp.Children[1];

        Action<object,EventArgs> tempEvent = (object a, EventArgs b) =>
        {
            popup.IsOpen = false;
            popup.IsOpen = true;
        };
        reset_Popus += new EventHandler(tempEvent);
        this.LocationChanged += new EventHandler(tempEvent);
  }

  // finally the method that I needed
  public void ResetAllPopups()
  {
      foreach (var d in reset_Popus.GetInvocationList())
      {
          d.DynamicInvoke(null, null);
      }
  }