work hard work smart

专注于Java后端开发。 不断总结,举一反三。
随笔 - 1158, 文章 - 0, 评论 - 153, 阅读 - 186万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF popup控件的使用

Posted on   work hard work smart  阅读(14798)  评论(0编辑  收藏  举报

 

<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);
       }
 
       private void Button_Click(object sender, RoutedEventArgs e)
       {
           MessageBox.Show("Test");
       }
   }

  

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示