相信很多人用过MessageBox.show(),是不是觉得这个消息框有点丑呢,反正我是觉得有点丑的,所以我自己重写了一个。先不说,上两幅图对比先:

  WPF 自定义消息框(转)WPF 自定义消息框(转)

当然,也不是很好看,不过比原有的好多了。

不多说了,先上xmal代码:

WPF 自定义消息框(转)
 1 <Window x:Class="MESBox.MEGBox"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MEGBox"  MinWidth="200"  WindowStyle="None"  
 5         AllowsTransparency="True" Background="#AA000000"
 6         WindowStartupLocation="CenterScreen" Window.SizeToContent="WidthAndHeight"
 7         MouseLeftButtonDown="DragWindow"  ShowInTaskbar="False">
 8     <Window.Resources>
 9         <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
10             <Setter Property="Foreground" Value="White"/>
11             <Setter Property="Template">
12                 <Setter.Value>
13                     <!--设置样式 -->
14                     <ControlTemplate TargetType="{x:Type Button}">
15                         <Grid>
16                             <Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="12.5" RadiusY="12.5" Fill="#FF777777">
17                             </Rectangle>
18                             <ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
19                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
20                         </Grid>
21                         <!-- 设置鼠标移到关闭按钮上的效果 -->
22                         <ControlTemplate.Triggers>
23                             <Trigger Property="IsMouseOver" Value="true">
24                                 <Setter Property="Fill" TargetName="Rectangle">
25                                     <Setter.Value>
26                                         <SolidColorBrush Color="White"></SolidColorBrush>
27                                     </Setter.Value>
28                                 </Setter>
29                                 <Setter Property="Foreground" Value="Black"></Setter>
30                             </Trigger>
31                         </ControlTemplate.Triggers>
32                     </ControlTemplate>
33                 </Setter.Value>
34             </Setter>
35         </Style>
36     </Window.Resources>
37 
38     <Grid  Height="Auto">
39         <Grid.RowDefinitions>
40             <RowDefinition Height="Auto"></RowDefinition>
41             <RowDefinition Height="Auto"></RowDefinition>
42             <RowDefinition Height="Auto" ></RowDefinition>
43         </Grid.RowDefinitions>
44         <DockPanel Grid.Row="0">
45             <Button DockPanel.Dock="Right" Style="{StaticResource ButtonStyle}" 
46                     Width="25" Height="25" Content="X" 
47                     HorizontalAlignment="Right" VerticalAlignment="Top" 
48                     Margin="3,3,3,3"
49                     Click="CloseWindow" >
50             </Button>
51         </DockPanel>
52         <TextBlock Padding="10,15,10,15" Grid.Row="1" x:Name="content" 
53                    Foreground="White" FontSize="18"
54                    MaxWidth="500" TextWrapping="Wrap"/>
55 
56         <StackPanel Orientation="Horizontal"  FlowDirection="RightToLeft"  Grid.Row="2">
57             <Button Content="确定" Width="80"   Click="CloseWindow" Height="30" Margin="10,0,0,0" ></Button>
58         </StackPanel>    
59     </Grid>
60 </Window>
WPF 自定义消息框(转)

相关文章: