2012-03-20 23 views
0

所有事件我有一個列表視圖火被預訂了特定的事件處理程序

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

反正是列表視圖被綁定到一個觀察的集合。我使用列上的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()方法,其中該方法將重置所有彈出窗口。如果有人滾動或出於飲水的原因,我想重置所有popus。

我知道我將能夠解決此問題:

通過在ListView的listviewitems迭代尋找具有VisualTreeHelper彈出控制然後重新找到的每一個彈出窗口。

我認爲如果我能找到訂閱this.LocationChanged事件處理程序的所有事件會更好。我怎樣才能執行所有被this.LocationChanged事件驅使的事件,就像我在哪裏改變窗口的位置一樣?

回答

1

你想得太多UI爲中心,只需綁定IsOpen到你的項目,然後在它們之間迭代,沒有必要讓一個Popup實例。

+0

如果我不使用彈出窗口內的列表視圖不會顯示出來。我使用該列表在主列表視圖中添加自動完成功能。或者,也許有一個屬性,我可以設置到堆疊面板,以便它的內容將顯示 – 2012-03-20 17:41:51

+0

@TonoNam:我的意思是,你不需要獲得對'Popups'的引用,當然是'Popups'本身應該保持,只需綁定'IsOpen'並更改綁定屬性。 – 2012-03-20 18:24:40

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