我有一个listview
<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 =&#34; xID_Loaded&#34; 事件来每次向listview添加新行时初始化弹出控件。
因此在我的代码背后我有:
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()
的方法,其中该方法将重置所有弹出窗口。万一有人滚动或为了watever原因我想重置所有popus。
我知道我可以通过以下方式解决这个问题:
遍历listview中的listviewitems,使用VisualTreeHelper查找弹出控件,然后重置找到的每个弹出窗口。
我认为如果能找到订阅this.LocationChanged事件处理程序的所有事件会更好。如何执行this.LocationChanged事件所包含的所有事件,就像我在哪里更改窗口的位置一样?
答案 0 :(得分:1)
您认为以UI为中心,只需将IsOpen
绑定到您的项目,然后对其进行迭代,无需获取Popup
实例。
答案 1 :(得分: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);
}
}