下面这种按钮就是我们要仿照的按钮样式

本次主要是将这些按钮排列成几行几列的形状,类似下图

一、方块按钮样式

<!--360方块按钮--><Style x:Key="BtnStyle360Block" TargetType="Button"><Setter Property="Background" Value="White"></Setter><Setter Property="Template"><Setter.Value><ControlTemplate  TargetType="{x:Type Button}"><Border x:Name="border" BorderThickness="1" BorderBrush="Transparent" Tag="{Binding}"><Grid><ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"></ContentPresenter></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="border" Value="#F2F2F2"/></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Background" TargetName="border" Value="LightGray"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

二、列表样式采用ListBox

1、ListBox自带的天蓝色选中背景我们要首先解决:

<!--ListBox没有选择背景--><Style x:Key="ListBoxItemNoSelectedColorSyle" TargetType="ListBoxItem"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ListBoxItem}"><Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"><ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/></Border><ControlTemplate.Triggers><Trigger Property="IsSelected" Value="true"><Setter Property="Foreground" Value="Transparent"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

2、ListBox多列显示、因为UniformGrid的特点就是排满行后自动换行。所以我们只设定列个数就好了

<!--ListBox 多列--><Style x:Key="ListBoxStyle" TargetType="ListBox"><Setter Property="Foreground" Value="Black"/><!--无选中颜色--><Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemNoSelectedColorSyle}"></Setter><!--模板容器样式--><Setter Property="ItemsPanel"><Setter.Value><ItemsPanelTemplate><UniformGrid Columns="3" IsItemsHost="True"></UniformGrid></ItemsPanelTemplate></Setter.Value></Setter><!--模板内容样式--><Setter Property="ItemTemplate"><Setter.Value><DataTemplate><Button Name="btn" Tag="{Binding}"  Height="76" Width="76" ToolTip="{Binding Name}" Style="{StaticResource BtnStyle360Block}"><Button.Content><StackPanel ><Image  Width="40" Height="40" Margin="0,5,0,0" Source="{Binding Image}" Stretch="Fill" HorizontalAlignment="Center"/><Label  Height="26" Foreground="{Binding ElementName=btn,Path=Foreground}" VerticalContentAlignment="Center" VerticalAlignment="Center" Content="{Binding Title}" Margin="0,0,0,-2"/></StackPanel></Button.Content></Button></DataTemplate></Setter.Value></Setter></Style>

三、关于如何使用这个样式

<ListBox  BorderThickness="0" ItemsSource="{Binding DownloadSoftModelList}" Style="{StaticResource ListBoxStyle}" Grid.Row="1" Grid.ColumnSpan="3" Background="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden"></ListBox>

四、关于按钮事件的问题

我如何获取ListBox选中的哪个按钮?要不然这个搞有什么意思?

经过我的多次试验。发现只有PreviewMouseUp 事件才会得到我们想要的按钮。

public void  SelectionChanged(object sender,object eventArgs)
<pre name="code" class="csharp">{<span style="white-space:pre">    </span>//只有PreviewMousUp才会触发事件 - 并且不会造成全局异常if (eventArgs.GetType() == typeof(System.Windows.Input.MouseButtonEventArgs)){<span style="white-space:pre">    </span>Button btn = (UIElement)((System.Windows.Input.MouseButtonEventArgs)eventArgs).OriginalSource as Button;if (btn != null){}}}

如果你想再进行多次试验试试到底哪种事件靠谱的话。不妨配合修改样式360ButtonBlock样式里面的控件IsHitTestVisible 属性。

来取消某些控件的焦点响应,从而事件的源也就不会是它;

WPF 方块按钮 仿照360相关推荐

  1. wpf 去除按钮点击时会画一个点线的矩形

    wpf 去除按钮点击时会画一个点线的矩形 最近在研究wpf,啥都不懂,特把自己查到的有用东西,收集一下: <Page xmlns="http://schemas.microsoft.c ...

  2. WPF圆角按钮与触发颜色变化

    原文:WPF圆角按钮与触发颜色变化 <Button x:Name="button1" Content="按钮1" Margin="10,10,0 ...

  3. WPF RadioButton按钮控件取消选中设置

    WPF RadioButton按钮控件取消选中设置 通常我们使用的RadioButton控件选中之后就不能进行取消选中的操作,我上网查找到了解决方法,如下: 首先我们在View视图这里写这个按钮的点击 ...

  4. android仿照360壁纸照片显示特效,Android仿360桌面手机卫士悬浮窗效果

    大家好,今天给大家带来一个仿360手机卫士悬浮窗效果的教程,在开始之前请允许我先说几句不相干的话. 不知不觉我发现自己接触Android已有近三个年头了,期间各种的成长少不了各位高手的帮助,总是有很多 ...

  5. Wpf拖动按钮实现(二)

    Main.xaml <Grid x:Name="LayoutRoot" Background="Transparent"><Grid.RowD ...

  6. WPF圆角按钮及触发背景变更_se7en3_新浪博客

    WPF Button自定义Template并在ControlTemplate.Triggers定义背景变更 转http://www.cnblogs.com/junbird-nest/archive/2 ...

  7. wpf异形按钮_WPF Window异形窗口演示

    我们先通过简单的效果展示,切换展示不同图片: 我们先定义图片资源文件,我们可以在window资源中定义,下面的在app.xaml文件来定义: xmlns="http://schemas.mi ...

  8. wpf异形按钮,定制异型按钮在WPF

    I have a requirement of creating a button which takes the shapes as displayed in the picture: Can an ...

  9. css美观文本框_如何为美观和用户友好的文本使用类型

    css美观文本框 Text is an important, if not the most important element in a design. It conveys the informa ...

最新文章

  1. python从入门到实践回顾——字典
  2. To connect to files.phpmyadmin.net insecurely, use `--no-check-certificate‘
  3. 解决: Error: Program type already present: android.support.v4.os.ResultReceiver$MyResultReceiver
  4. login控件authenticate_Login.Authenticate 事件 (System.Web.UI.WebControls) | Microsoft Docs
  5. 一天搞定CSS:层级(z-index)--18
  6. vim之terminal之窗口大小命令
  7. [Linux]Ubuntu 以管理员权限打开文件夹
  8. 形参和实参是什么?? shim和polyfil是什么意思??
  9. linux init 参数,Objective-C中实现覆写init函数以及在初始化时添加参数
  10. component多个 vue_VUE多个组件示例
  11. webpack多环境(dev stg prd qa)打包问题
  12. TeaVM当前版本(0.7.0)还不支持传递字串参数
  13. 从SVN上拉取代码到本地进行开发
  14. origin2018软件下载和安装教程
  15. iphone个系列尺寸_iphone各机型尺寸
  16. linux tao环境 安装_菜鸟Linux 编译TAO2.0a的问题,求高手指导
  17. 免费的桌面主题按钮 V1.0
  18. 什么是 C# 分部类(partia)
  19. 为什么点开桌面计算机那这么慢,电脑桌面刷新反应很慢怎么办?电脑桌面刷新很慢解决方法...
  20. Vue在线预览ppt,可嵌入组件显示也可新开页面显示

热门文章

  1. 海尔简爱s11装Linux,海尔简爱S11值得买吗 海尔简爱S11全面深度评测详解
  2. 评价类(观点)题和理解类题目的辨析 区别一:审题干信息和题目要求。
  3. 信息与通信的数学基础——第十三章 行波法
  4. 存储单位bit、byte、KB、MB、GB、TB、PB、EB、ZB、YB、BB、NB、DB、CB、XB
  5. 9139 位艺人在 Python 面前不值一提 # Python 爬虫小课 5-9
  6. SwiftUI——全局背景渐变色(Gradient)案例
  7. 手动双面打印机-打印规则
  8. 如何在 Python 中构建跨平台桌面应用程序
  9. 十一、海思HISI3556V200摄像头功能裁剪
  10. 大学计算机第四讲答案,大学职业生涯规划课第四讲答案