WPF界面设计中常用的一些代码片段及属性
一、窗体去掉标题栏、消除掉标题栏后的白边,把窗体置于屏幕中间,窗口大小不能改变。
1 WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"
二、窗口可拖动
1 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) 2 { 3 base.OnMouseLeftButtonDown(e); 4 5 // 获取鼠标相对标题栏位置 6 Point position = e.GetPosition(gridTitleBar);//gridTiltleBar一般是最外层控件的Name属性即可。 7 8 // 如果鼠标位置在标题栏内,允许拖动 9 if (e.LeftButton == MouseButtonState.Pressed) 10 { 11 if (position.X >= 0 && position.X < gridTitleBar.ActualWidth && position.Y >= 0 && position.Y < gridTitleBar.ActualHeight) 12 { 13 this.DragMove(); 14 } 15 } 16 }
三、设置Image元素的图片。
1 BitmapImage image = new BitmapImage(new Uri(@"./Images/pic_01_4.png", UriKind.Relative));//@"./Images/pic_01_4.png"为图片路径 2 this.img_Camera.Source = image;//img_Camera为image的Name属性值
四、按钮的样式(鼠标hover时的样式,鼠标按下时的样式,以及普通状态下的样式),把这段代码放到App.xaml的<Application.Resources>节点下。
1 <Style x:Key="SltBtnMinimized" TargetType="{x:Type Button}"> 2 <Setter Property="Template"> 3 <Setter.Value> 4 <ControlTemplate TargetType="{x:Type Button}"> 5 <Border> 6 <Image x:Name="imgContent" Source="Images/wintl_mini_normal.png" /> 7 </Border> 8 <ControlTemplate.Triggers> 9 <Trigger Property="IsMouseOver" Value="True"> 10 <Setter TargetName="imgContent" Property="Source" Value="Images/wintl_mini_hover.png"/> 11 </Trigger> 12 <Trigger Property="IsPressed" Value="True"> 13 <Setter TargetName="imgContent" Property="Source" Value="Images/wintl_mini_press.png"/> 14 </Trigger> 15 </ControlTemplate.Triggers> 16 </ControlTemplate> 17 </Setter.Value> 18 </Setter> 19 </Style>
引用的时候,
1 <Button Width="30" Height="26" x:Name="btn_Close" ToolTip="关闭" Style="{StaticResource BtnCloseStyle}"/>
五、复选框
1 <Style x:Key="CheckBoxTemplate" TargetType="{x:Type CheckBox}"> 2 <Setter Property="Template"> 3 <Setter.Value> 4 <ControlTemplate TargetType="{x:Type CheckBox}"> 5 <StackPanel Orientation="Horizontal"> 6 <Border> 7 <Image x:Name="imageCheckBox" Source="Resources/check_normal.png" Width="18" Height="18" /> 8 </Border> 9 <ContentPresenter VerticalAlignment="Center" /> 10 </StackPanel> 11 <ControlTemplate.Triggers> 12 <Trigger Property="IsChecked" Value="False"> 13 <Setter TargetName="imageCheckBox" Property="Source" Value="Resources/check_normal.png" /> 14 </Trigger> 15 <Trigger Property="IsChecked" Value="True"> 16 <Setter TargetName="imageCheckBox" Property="Source" Value="Resources/check_press.png" /> 17 </Trigger> 18 <Trigger Property="IsChecked" Value="{x:Null}" > 19 <Setter TargetName="imageCheckBox" Property="Source" Value="/Resources/check_hover.png" /> 20 </Trigger> 21 </ControlTemplate.Triggers> 22 </ControlTemplate> 23 </Setter.Value> 24 </Setter> 25 </Style>
六、下拉箭头
<CheckBox Name="chkMore" HorizontalAlignment="Right" Margin="0,0,65,0" VerticalAlignment="Center" Style="{StaticResource MoreCheckBoxTemplate}" LostFocus="chkMore_LostFocus"/> <Popup Width="60" IsOpen="{Binding ElementName=chkMore,Path=IsChecked}" PlacementTarget="{Binding ElementName=chkMore}" AllowsTransparency="True" PopupAnimation="Fade"> <WrapPanel Width="60"> <Button Name="btnMenuSetting" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource SettingMenuButtonTemplate}" Click="btnMenuSetting_Click"/> <Button Name="btnMenuAbout" HorizontalAlignment="Right" VerticalAlignment="Center" Style="{StaticResource AboutMenuButtonTemplate}" Click="btnMenuAbout_Click"/> </WrapPanel> </Popup>
checkbox对应的Style在App.xmal中
1 <Style x:Key="MoreCheckBoxTemplate" TargetType="{x:Type CheckBox}"> 2 <Setter Property="Template"> 3 <Setter.Value> 4 <ControlTemplate TargetType="{x:Type CheckBox}"> 5 <StackPanel Orientation="Horizontal" Height="28"> 6 <Border> 7 <Image x:Name="imageCheckBox" Source="Resources/btn_more_normal.png" Width="28" Height="28" /> 8 </Border> 9 <ContentPresenter VerticalAlignment="Center" /> 10 </StackPanel> 11 <ControlTemplate.Triggers> 12 <Trigger Property="IsChecked" Value="False"> 13 <Setter TargetName="imageCheckBox" Property="Source" Value="Resources/btn_more_normal.png" /> 14 </Trigger> 15 <Trigger Property="IsChecked" Value="True"> 16 <Setter TargetName="imageCheckBox" Property="Source" Value="Resources/btn_more_press.png" /> 17 </Trigger> 18 <Trigger Property="IsChecked" Value="{x:Null}" > 19 <Setter TargetName="imageCheckBox" Property="Source" Value="/Resources/btn_more_hover.png" /> 20 </Trigger> 21 </ControlTemplate.Triggers> 22 </ControlTemplate> 23 </Setter.Value> 24 </Setter> 25 </Style>
七、控件无边框
1 BorderThickness="0"
八、控件底色透明
1 Background="Transparent"
九、渐变色GradientBrush画圆
1 <Ellipse Width="120" Height="120"> 2 <Ellipse.Fill> 3 <RadialGradientBrush GradientOrigin="0.25,0.25" RadiusX="0.75" RadiusY="0.75"> 4 <RadialGradientBrush.GradientStops> 5 <GradientStop Offset="0" Color="White"></GradientStop> 6 <GradientStop Offset="0.65" Color="Black"></GradientStop> 7 <GradientStop Offset="0.8" Color="Gray"></GradientStop> 8 </RadialGradientBrush.GradientStops> 9 </RadialGradientBrush> 10 </Ellipse.Fill> 11 </Ellipse>
效果如下: