WPF中Popup控制項的使用
一、Popup控制項的主要屬性
Popup表示具有內容的彈出窗口,其主要屬性為:
- Child:獲取或設置 Popup控制項的內容。
- IsOpen:獲取或設置一個值,該值指示Popup 是否可見
- Placement:獲取或設置 Popup 控制項打開時的控制項方向,並指定Popup 控制項在與螢幕邊界重疊時的控制項行為
- PlacementTarget:獲取或設置當打開 Popup 控制項時該控制項相對於其放置的元素。
- PopupAnimation:獲取或設置Popup 控制項的打開和關閉動畫。
- StaysOpen:獲取或設置一個值,該值指示當 Popup 控制項焦點不再對準時,是否關閉該控制項。
Popup主要事件為:
-
Opened:當IsOpen 屬性更改為
true
時發生。private void PopupOpening(object sender, EventArgs e) { //Code to execute when Popup opens }
-
Closed:當IsOpen 屬性更改為
false
時發生。private void PopupClosing(object sender, EventArgs e) { //Code to execute when Popup closes }
二、Popup控制項使用
我們使用Popup實現一個功能:當文本框獲取到焦點時,彈出下拉框,裡面選擇水果種類,具體效果如下圖所示:
相關的介面程式碼為:
<Window.Resources>
<x:Array x:Key="MyArray" Type="system1:String">
<system1:String>西瓜</system1:String>
<system1:String>葡萄</system1:String>
<system1:String>芒果</system1:String>
<system1:String>獼猴桃</system1:String>
</x:Array>
</Window.Resources>
<Grid Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Height="30" BorderThickness="1"
VerticalContentAlignment="Center"
Padding="10,0"
x:Name="TestTextBox"
ToolTip="{StaticResource MyArray}">
</TextBox>
<Button Grid.Column="1" Content="搜索" Margin="10,0" Height="30" Click="ButtonBase_OnClick"></Button>
<Popup Grid.Column="0"
AllowsTransparency="True"
PopupAnimation="Slide"
PlacementTarget="{Binding ElementName=TestTextBox}"
Placement="Bottom"
Width="{Binding ElementName=TestTextBox, Path=ActualWidth}"
IsOpen="{Binding ElementName=TestTextBox, Path=IsKeyboardFocused, Mode=OneWay}">
<ListBox ItemsSource="{Binding ElementName=TestTextBox, Path=ToolTip}"
SelectedItem="{Binding ElementName=TestTextBox, Path=Text, Mode=OneWayToSource}"></ListBox>
</Popup>
</Grid>
為了,讓Popup控制項能夠跟隨目標控制項移動,需要增加一個工具類,具體如下所示:
public class PopupHelper
{
/// <summary>
/// 附加屬性:跟隨放置控制項而移動
/// </summary>
public static readonly DependencyProperty PopupPlacementTargetProperty = DependencyProperty.RegisterAttached("PopupPlacementTarget", typeof(DependencyObject), typeof(PopupHelper), new PropertyMetadata(null, OnPopupPlacementTargetChanged));
public static DependencyObject GetPopupPlacementTarget(DependencyObject obj)
{
return (DependencyObject)obj.GetValue(PopupPlacementTargetProperty);
}
public static void SetPopupPlacementTarget(DependencyObject obj, DependencyObject value)
{
obj.SetValue(PopupPlacementTargetProperty, value);
}
private static void OnPopupPlacementTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Controls.Primitives.Popup pop && e.NewValue is DependencyObject placementTarget)
{
var window = Window.GetWindow(placementTarget);
if (window != null)
{
window.LocationChanged += delegate
{
var mi = typeof(System.Windows.Controls.Primitives.Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
mi?.Invoke(pop, null);
};
}
}
}
}
該附加屬性的使用如下所示:
<Popup local:PopupHelper.PopupPlacementTarget="{Binding ElementName=TestTextBox}"></Popup>