欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析WPF中Popup彈出層的使用

 更新時(shí)間:2024年01月11日 10:07:28   作者:老碼識(shí)途呀  
這篇文章將通過(guò)一個(gè)簡(jiǎn)單的小例子,為大家詳細(xì)介紹一下如何在WPF開(kāi)發(fā)中,通過(guò)Popup實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊彈出浮動(dòng)??看翱?有需要的小伙伴可以了解下

在日常開(kāi)發(fā)中,當(dāng)點(diǎn)擊某控件時(shí),經(jīng)??吹揭恍棾隹颍?吭谀承╉?yè)面元素的附近,但這些又不是真正的窗口,而是頁(yè)面的一部分,那這種功能是如何實(shí)現(xiàn)的呢?今天就以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何在WPF開(kāi)發(fā)中,通過(guò)Popup實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊彈出浮動(dòng)??看翱?/strong>

什么是Popup

Popup(彈出層),Popup 控件提供一種在單獨(dú)窗口中顯示內(nèi)容的方法,該窗口相對(duì)于指定元素或屏幕坐標(biāo)在當(dāng)前應(yīng)用程序窗口上浮動(dòng)。  Popup控件通過(guò)IsOpen屬性控制是否可見(jiàn)。 當(dāng)可見(jiàn)時(shí),IsOpen 屬性設(shè)置為 true,否則為 false。

創(chuàng)建Popup

在WPF程序開(kāi)發(fā)中,Popup和其他控件一樣,可以定義控件的內(nèi)容,創(chuàng)建Popup示例如下所示:

<ToggleButton x:Name="TogglePopupButton" Height="30" Width="150" HorizontalAlignment="Left">
	<StackPanel>
		<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">
			<Run Text="是否狀態(tài)切換? " />
			<Run Text="{Binding IsChecked, ElementName=TogglePopupButton}" />
		</TextBlock>
		<Popup Name="myPopup" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton}">
			<Border BorderThickness="1">
				<TextBlock Name="myPopupText" Text="這是一段彈出內(nèi)容" Background="LightBlue" Foreground="Blue" Padding="30"></TextBlock>
			</Border>
		</Popup>
	</StackPanel>
</ToggleButton>

注意:上述示例,當(dāng)ToggleButton被選中時(shí),彈出提示內(nèi)容;當(dāng)ToggleButton取消選中時(shí),隱藏彈出層。主要是將Popup的IsOpen屬性和ToggleButton的IsChecked屬性進(jìn)行綁定。

示例截圖效果如下所示:

Popup的行為

通過(guò)Popup的IsOpen屬性,來(lái)控制彈出層的顯示與隱藏,當(dāng)打開(kāi)或關(guān)閉 Popup 內(nèi)容窗口時(shí),將引發(fā) Opened 和 Closed 事件。默認(rèn)情況下,當(dāng)IsOpen屬性為true時(shí),將一直處于打開(kāi)狀態(tài),直到屬性變?yōu)閒alse。但是也可以通過(guò)StaysOpen屬性設(shè)置來(lái)判斷Popup是否處于focus狀態(tài)而決定是否顯示,當(dāng)StaysOpen屬性為false時(shí),如果Popup失去焦點(diǎn),將會(huì)隱藏。StaysOpen默認(rèn)值為true。

Popup動(dòng)畫(huà)

Popup控件,默認(rèn)支持淡入,滑入等動(dòng)畫(huà)效果,可通過(guò)PopupAnimation屬性進(jìn)行設(shè)置,并且設(shè)置AllowsTransparency為true。除了之處默認(rèn)的淡入,滑入等動(dòng)畫(huà)效果外,還可以通過(guò)自定義StoryBoard來(lái)實(shí)現(xiàn)動(dòng)畫(huà)。

<CheckBox x:Name="myCheckBox" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"></CheckBox>
<Popup Grid.Column="0" IsOpen="{Binding ElementName=myCheckBox,Path=IsChecked}" PlacementTarget="{Binding ElementName=myCheckBox}" AllowsTransparency="True" PopupAnimation="Slide" HorizontalOffset="50" VerticalOffset="20" Margin="10" Width="200" Height="200">
	<Canvas Width="100" Height="100" Background="DarkBlue">
		<TextBlock TextWrapping="Wrap" Foreground="White" Text="旋轉(zhuǎn)Popup" VerticalAlignment="Center" HorizontalAlignment="Center" Canvas.Top="40" Canvas.Left="18"></TextBlock>
	</Canvas>
</Popup>

上述示例,設(shè)置了Popup的兩個(gè)屬性【AllowsTransparency="True" PopupAnimation="Slide"】來(lái)實(shí)現(xiàn)Popup的滑入效果,如下所示:

定義Popup的位置

Popup作為彈出層,可以相對(duì)頁(yè)面上的控件元素進(jìn)行定位,也可以相關(guān)整個(gè)窗口進(jìn)行定位。

1. 通過(guò)PlacementTarget和Placement進(jìn)行定位

PlacementTarget為Popup指定相對(duì)定位的目標(biāo)對(duì)象。如果已設(shè)置 PlacementTarget 屬性,則它指定目標(biāo)對(duì)象。 如果未設(shè)置 PlacementTarget 并且 Popup 具有父級(jí),則父級(jí)就是目標(biāo)對(duì)象。 如果沒(méi)有 PlacementTarget 值并且沒(méi)有父級(jí),則沒(méi)有目標(biāo)對(duì)象并且 Popup 相對(duì)于屏幕進(jìn)行定位。Placement為枚舉類型,常用的有Bottom,Top,Left,Right等,示例如下所示:

<Button x:Name="button1" Grid.Column="1" Width="120" Height="100" Content="目標(biāo)對(duì)象"></Button>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=button1}" Placement="Bottom">
	<TextBlock FontSize="14" Background="LightGreen">底部</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=button1}" Placement="Top">
	<TextBlock FontSize="14" Background="LightGreen">頂部</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=button1}" Placement="Left">
	<TextBlock FontSize="14" Background="LightGreen">左側(cè)</TextBlock>
</Popup>
<Popup IsOpen="True" PlacementTarget="{Binding ElementName=button1}" Placement="Right">
	<TextBlock FontSize="14" Background="LightGreen">右側(cè)</TextBlock>
</Popup>

示例效果如下所示:

2. PlacementRectangle目標(biāo)區(qū)域進(jìn)行定位

除此之外,還可以通過(guò)PlacementRectangle設(shè)置目標(biāo)區(qū)域

目標(biāo)區(qū)域示例如下所示:

<StackPanel Orientation="Horizontal" Grid.Row="1">
 
	<Canvas Width="200" Height="100" Background="Red">
		<Rectangle Canvas.Top="30" Canvas.Left="30" Width="50" Height="50" Stroke="White" StrokeThickness="3"/>
		<Popup IsOpen="True" PlacementRectangle="30,30,30,50">
			<TextBlock FontSize="14" Background="Yellow" Width="140" TextWrapping="Wrap" Text="這是通過(guò)PlacementRectangle定位的Popup"></TextBlock>
		</Popup>
	</Canvas>
 
	<Canvas Width="200" Height="100" Background="Red" Margin="30,0,0,0">
		<Rectangle Canvas.Top="30" Canvas.Left="50" Width="50" Height="50" Stroke="White" StrokeThickness="3"/>
		<Popup IsOpen="True">
			<TextBlock FontSize="14" Background="Yellow" Width="140" TextWrapping="Wrap" Text="這是沒(méi)有通過(guò)PlacementRectangle定位的Popup"></TextBlock>
		</Popup>
	</Canvas>
 
</StackPanel>

以上示例通過(guò)設(shè)置PlacementRectangle屬性來(lái)目標(biāo)區(qū)域【目標(biāo)區(qū)域并不會(huì)真的可見(jiàn)】。沒(méi)有設(shè)置則以父類Canvas進(jìn)行??俊H缦滤?/p>

3. 通過(guò)HorizontalOffset 和 VerticalOffset設(shè)置偏移進(jìn)行定位

不僅可以設(shè)置目標(biāo)區(qū)域,還可以通過(guò)HorizontalOffset 和 VerticalOffset 屬性使 Popup 從目標(biāo)區(qū)域偏移等。

<Canvas Width="200" Height="200" Background="Yellow" Margin="20">
  <Popup IsOpen="True" Placement="Bottom"
         HorizontalOffset="20" VerticalOffset="20">
    <TextBlock FontSize="14" Background="#42F3FD">
      This is a popup.
    </TextBlock>
  </Popup>
</Canvas>

偏移示例,如下所示:

以上就是淺析WPF中Popup彈出層的使用的詳細(xì)內(nèi)容,更多關(guān)于WPF Popup彈出層的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論