<Window x:Class= "WPFPopup.RuntimePopup" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" Title= "RuntimePopup" Height= "800" Width= "800" Loaded= "Window_Loaded" > <StackPanel> <Button Height= "300" >T</Button> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <CheckBox Name= "PCheckBox" Width= "100" Margin= "60,10,0,0" Content= "Popup Window" /> <CheckBox Name= "PCheckBox2" Grid.Column= "1" Width= "100" Margin= "0,10,0,0" Content= "Popup Window" /> </Grid> <Button HorizontalAlignment= "Left" Width= "169" Margin= "10,10,0,0" > <Button.Triggers> <EventTrigger RoutedEvent= "Button.Click" > <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName= "theTransform" Storyboard.TargetProperty= "(RotateTransform.Angle)" From= "0" To= "360" Duration= "0:0:5" AutoReverse= "True" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> Start Animation </Button> <Popup x:Name= "popusBottom" IsOpen= "{Binding ElementName=PCheckBox,Path=IsChecked}" PlacementTarget= "{Binding ElementName=PCheckBox}" AllowsTransparency= "True" PopupAnimation= "Fade" HorizontalOffset= "5" VerticalOffset= "-120" > <Canvas Margin= "150" Background= "Green" > <Canvas.RenderTransform> <RotateTransform x:Name= "theTransform" /> </Canvas.RenderTransform> <WrapPanel > <WrapPanel.Background> <ImageBrush ImageSource= "BG.png" /> </WrapPanel.Background> <Button Click= "Button_Click" Margin= "15" >Test</Button> <ListBox Height= "90" Margin= "15" > <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> </ListBox> </WrapPanel> </Canvas> </Popup> <Popup x:Name= "popusBottom2" IsOpen= "{Binding ElementName=PCheckBox2,Path=IsChecked}" PlacementTarget= "{Binding ElementName=PCheckBox2}" AllowsTransparency= "True" PopupAnimation= "Fade" HorizontalOffset= "5" VerticalOffset= "-120" > <Canvas Margin= "150" > <WrapPanel > <WrapPanel.Background> <ImageBrush ImageSource= "BG.png" /> </WrapPanel.Background> <Button Click= "Button_Click" Margin= "15" >Test</Button> <ListBox Height= "90" Margin= "15" > <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> <ListBoxItem>Item1</ListBoxItem> <ListBoxItem>Item2</ListBoxItem> <ListBoxItem>Item3</ListBoxItem> </ListBox> </WrapPanel> </Canvas> </Popup> </StackPanel> </Window> |
C# code
public partial class RuntimePopup : Window { public RuntimePopup() { InitializeComponent(); LocationChanged += new EventHandler(RuntimePopup_LocationChanged); } void RuntimePopup_LocationChanged( object sender, EventArgs e) { var mi = typeof (Popup).GetMethod( "UpdatePosition" , System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); mi.Invoke(popusBottom, null );//控制popup随window移动而移动 } private void Button_Click( object sender, RoutedEventArgs e) { MessageBox.Show( "Test" ); } } |