最近为了实现WPF中弹框组件写了一个小例子:
组件要求:
1.自定义标题
2自定义标题颜色
3提供关闭按钮,
4.弹框内容可由调用方自行嵌入
xaml代码
<UserControl x:Class="WpfApp1.Controls.CustomPopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1.Controls"
MouseLeftButtonDown="UserControl_MouseLeftButtonDown"
MouseLeftButtonUp="UserControl_MouseLeftButtonUp"
MouseMove="UserControl_MouseMove" Height="142" Width="112">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<!-- 标题栏 -->
<Border Grid.Row="0" Background="{Binding TitleBackground, RelativeSource={RelativeSource AncestorType={x:Type local:CustomPopup}}}">
<DockPanel>
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type local:CustomPopup}}}"
Foreground="White" FontWeight="Bold" Margin="5" Width="46"/>
<Button x:Name="CloseButton" Content="X" DockPanel.Dock="Right" Margin="5" Click="CloseButton_Click"
Visibility="{Binding ShowCloseButton, RelativeSource={RelativeSource AncestorType={x:Type local:CustomPopup}}, Converter={StaticResource BooleanToVisibilityConverter}}" Width="30" Height="25" HorizontalAlignment="Right"/>
</DockPanel>
</Border>
<!-- 内容区域 -->
<ContentPresenter Grid.Row="1" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:CustomPopup}}, Path=Content}"/>
</Grid>
</Border>
</UserControl>
cs代码
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WpfApp1.Models;
namespace WpfApp1.Controls
{
public partial class CustomPopup : UserControl
{
private bool isDragging;
private Point offset;
public CustomPopup()
{
InitializeComponent();
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
offset = e.GetPosition(this);
CaptureMouse();
// 设置当前弹框的 Panel.ZIndex 为最高值
GlobalSetting.CurrentPageIndex++;
Panel.SetZIndex(this, GlobalSetting.CurrentPageIndex);
}
private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
ReleaseMouseCapture();
}
private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
var parent = VisualTreeHelper.GetParent(this) as UIElement;
if (parent != null)
{
Point mousePos = e.GetPosition(parent);
double newX = mousePos.X - offset.X;
double newY = mousePos.Y - offset.Y;
Canvas.SetLeft(this, newX);
Canvas.SetTop(this, newY);
}
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
var parent = VisualTreeHelper.GetParent(this);
if (parent is Panel panel)
{
panel.Children.Remove(this);
}
}
public static DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(CustomPopup));
public static DependencyProperty TitleBackgroundProperty =
DependencyProperty.Register("TitleBackground", typeof(Brush), typeof(CustomPopup));
public static DependencyProperty ShowCloseButtonProperty =
DependencyProperty.Register("ShowCloseButton", typeof(bool), typeof(CustomPopup));
public static DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(object), typeof(CustomPopup));
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public Brush TitleBackground
{
get { return (Brush)GetValue(TitleBackgroundProperty); }
set { SetValue(TitleBackgroundProperty, value); }
}
public bool ShowCloseButton
{
get { return (bool)GetValue(ShowCloseButtonProperty); }
set { SetValue(ShowCloseButtonProperty, value); }
}
public object Content
{
get { return GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
}
}
其他页面调用时文章来源:https://www.toymoban.com/news/detail-565565.html
<Canvas Margin="0,125,0,0" x:Name="mainBody">
<Controls:CustomPopup Width="416" Height="200" Title="Custom Popup" TitleBackground="LightBlue" ShowCloseButton="True" Canvas.Left="199" Canvas.Top="224" HorizontalAlignment="Left" VerticalAlignment="Top">
<Controls:CustomPopup.Content>
<!-- 自定义弹框组件的内容区域 -->
<StackPanel>
<TextBlock Text="This is the custom content!" FontSize="20" HorizontalAlignment="Center"/>
<Button Content="Click me!" Width="100" Height="30" HorizontalAlignment="Center" Margin="10"/>
</StackPanel>
</Controls:CustomPopup.Content>
</Controls:CustomPopup>
</Canvas>文章来源地址https://www.toymoban.com/news/detail-565565.html
到了这里,关于WPF自定义嵌入弹框控件,支持内容标题自定义的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!