UWP 自定义RadioButton实现Tab底部导航
先看效果:

参照Android的实现方式用RadioButton来实现,但是Uwp的RadioButton并没有安卓的Selector选择器
下面是一个比较简单的实现,如果有同学有更好的实现,欢迎留言,让我们共同进步。
1、首先自定义一个RadioImageButton控件,并定义几个依赖属性,代码如下
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Windows.UI;
5 using Windows.UI.Xaml;
6 using Windows.UI.Xaml.Controls;
7 using Windows.UI.Xaml.Media;
8
9 namespace Demo.UWP.Controls
10 {
11 public class RadioImageButton : RadioButton
12 {
13 //默认图片
14 public ImageSource Source
15 {
16 get { return (ImageSource)GetValue(SourceProperty); }
17 set { SetValue(SourceProperty, value); }
18 }
19
20 // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc...
21 public static readonly DependencyProperty SourceProperty =
22 DependencyProperty.Register("Source", typeof(ImageSource), typeof(RadioImageButton), null);
23
24 //选中图片
25 public ImageSource SourceChecked
26 {
27 get { return (ImageSource)GetValue(SourceCheckedProperty); }
28 set { SetValue(SourceCheckedProperty, value); }
29 }
30
31 // Using a DependencyProperty as the backing store for SourceChecked. This enables animation, styling, binding, etc...
32 public static readonly DependencyProperty SourceCheckedProperty =
33 DependencyProperty.Register("SourceChecked", typeof(ImageSource), typeof(RadioImageButton), null);
34
35 //选中文字颜色
36 public SolidColorBrush ForegroundChecked
37 {
38 get { return (SolidColorBrush)GetValue(ForegroundCheckedProperty); }
39 set { SetValue(ForegroundCheckedProperty, value); }
40 }
41
42 // Using a DependencyProperty as the backing store for ForegroundChecked. This enables animation, styling, binding, etc...
43 public static readonly DependencyProperty ForegroundCheckedProperty =
44 DependencyProperty.Register("ForegroundChecked", typeof(SolidColorBrush), typeof(RadioImageButton), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
45
46 //图片宽度
47 public double ImageWidth
48 {
49 get { return (double)GetValue(ImageWidthProperty); }
50 set { SetValue(ImageWidthProperty, value); }
51 }
52
53 // Using a DependencyProperty as the backing store for ImageWidth. This enables animation, styling, binding, etc...
54 public static readonly DependencyProperty ImageWidthProperty =
55 DependencyProperty.Register("ImageWidth", typeof(double), typeof(RadioImageButton), new PropertyMetadata(50));
56
57
58
59 public double ImageHeight
60 {
61 get { return (double)GetValue(ImageHeightProperty); }
62 set { SetValue(ImageHeightProperty, value); }
63 }
64
65 // Using a DependencyProperty as the backing store for ImageHeight. This enables animation, styling, binding, etc...
66 public static readonly DependencyProperty ImageHeightProperty =
67 DependencyProperty.Register("ImageHeight", typeof(double), typeof(RadioImageButton), new PropertyMetadata(50));
68
69
70
71 public Thickness ImageMargin
72 {
73 get { return (Thickness)GetValue(ImageMarginProperty); }
74 set { SetValue(ImageMarginProperty, value); }
75 }
76
77 // Using a DependencyProperty as the backing store for ImageMargin. This enables animation, styling, binding, etc...
78 public static readonly DependencyProperty ImageMarginProperty =
79 DependencyProperty.Register("ImageMargin", typeof(Thickness), typeof(RadioImageButton), null);
80
81
82 }
83 }
2、使用Blend工具生成RadioButton的模板,并修改其中的Grid,删除无用代码,添加一个Image控件,代码如下
1 <ResourceDictionary
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:local="using:Demo.UWP"
6 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7 xmlns:mycontrols="using:Demo.UWP.Controls"
8 mc:Ignorable="d">
9 <Style x:Key="RadioImageButtonStyle1" TargetType="mycontrols:RadioImageButton">
10 <Setter Property="Background" Value="{ThemeResource RadioButtonBackground}" />
11 <Setter Property="Foreground" Value="{ThemeResource RadioButtonForeground}" />
12 <Setter Property="BorderBrush" Value="{ThemeResource RadioButtonBorderBrush}" />
13 <Setter Property="Padding" Value="0" />
14 <Setter Property="HorizontalAlignment" Value="Left" />
15 <Setter Property="VerticalAlignment" Value="Center" />
16 <Setter Property="HorizontalContentAlignment" Value="Center" />
17 <Setter Property="VerticalContentAlignment" Value="Top" />
18 <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
19 <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
20 <Setter Property="MinWidth" Value="0" />
21 <Setter Property="UseSystemFocusVisuals" Value="True" />
22 <Setter Property="FocusVisualMargin" Value="-7,-3,-7,-3" />
23 <Setter Property="Template">
24 <Setter.Value>
25 <ControlTemplate TargetType="mycontrols:RadioImageButton">
26 <Grid
27 x:Name="RootGrid"
28 Background="{TemplateBinding Background}"
29 BorderBrush="{TemplateBinding BorderBrush}"
30 BorderThickness="{TemplateBinding BorderThickness}">
31 <Grid.RowDefinitions>
32 <RowDefinition Height="auto" />
33 <RowDefinition Height="*" />
34 </Grid.RowDefinitions>
35 <Image
36 x:Name="ImageFront"
37 Width="{TemplateBinding ImageWidth}"
38 Height="{TemplateBinding ImageHeight}"
39 Margin="{TemplateBinding ImageMargin}"
40 HorizontalAlignment="Center"
41 VerticalAlignment="Center"
42 Source="{TemplateBinding Source}"
43 Stretch="Uniform" />
44 <ContentPresenter
45 x:Name="ContentPresenter"
46 Grid.Row="1"
47 Margin="{TemplateBinding Padding}"
48 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
49 VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
50 AutomationProperties.AccessibilityView="Raw"
51 Content="{TemplateBinding Content}"
52 ContentTemplate="{TemplateBinding ContentTemplate}"
53 ContentTransitions="{TemplateBinding ContentTransitions}"
54 Foreground="{TemplateBinding Foreground}"
55 TextWrapping="Wrap" />
56 <VisualStateManager.VisualStateGroups>
57 <VisualStateGroup x:Name="CheckStates">
58 <VisualState x:Name="Checked">
59 <VisualState.Setters>
60 <!--<Setter Target="ImageBack.Visibility" Value="Visible"/>
61 <Setter Target="ImageFront.Visibility" Value="Collapsed"/>-->
62 <Setter Target="ImageFront.Source" Value="{Binding SourceChecked, RelativeSource={RelativeSource TemplatedParent}}" />
63 <Setter Target="ContentPresenter.Foreground" Value="{Binding ForegroundChecked, RelativeSource={RelativeSource TemplatedParent}}" />
64 </VisualState.Setters>
65 <!--<Storyboard>
66 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageBack" Storyboard.TargetProperty="Visibility">
67 <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
68 </ObjectAnimationUsingKeyFrames>
69 <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageFront" Storyboard.TargetProperty="Visibility">
70 <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
71 </ObjectAnimationUsingKeyFrames>
72 </Storyboard>-->
73 </VisualState>
74 <VisualState x:Name="Unchecked" />
75 <VisualState x:Name="Indeterminate" />
76 </VisualStateGroup>
77 </VisualStateManager.VisualStateGroups>
78 </Grid>
79 </ControlTemplate>
80 </Setter.Value>
81 </Setter>
82 </Style>
83 </ResourceDictionary>
用VisualStateManager实现选中状态的实现,56-77行代码,这里Setter的Value并不能用TemplateBinding进行绑定,点击是会报一个Value的异常
3、下面就开始使用了,直接上代码
1 <Grid>
2 <Grid.ColumnDefinitions>
3 <ColumnDefinition Width="auto" />
4 <ColumnDefinition Width="auto" />
5 <ColumnDefinition Width="auto" />
6 <ColumnDefinition Width="auto" />
7 </Grid.ColumnDefinitions>
8 <mycontrols:RadioImageButton
9 Grid.Row="0"
10 Margin="10"
11 Checked="RadioImageButton_Checked"
12 Content="首页"
13 FontSize="12"
14 FontWeight="Normal"
15 ForegroundChecked="Orange"
16 ImageHeight="40"
17 ImageMargin="5"
18 ImageWidth="40"
19 Source="ms-appx:///Assets/Main/main_index_home_normal.png"
20 SourceChecked="ms-appx:///Assets/Main/main_index_home_pressed.png"
21 Style="{StaticResource RadioImageButtonStyle1}" />
22 <mycontrols:RadioImageButton
23 Grid.Column="1"
24 Margin="10"
25 Content="品质优惠"
26 FontSize="12"
27 FontWeight="Normal"
28 ForegroundChecked="Orange"
29 ImageHeight="40"
30 ImageMargin="5"
31 ImageWidth="40"
32 Source="ms-appx:///Assets/Main/main_index_quality_normal.png"
33 SourceChecked="ms-appx:///Assets/Main/main_index_quality_pressed.png"
34 Style="{StaticResource RadioImageButtonStyle1}" />
35 <mycontrols:RadioImageButton
36 Grid.Column="2"
37 Margin="10"
38 Content="发现"
39 FontSize="12"
40 FontWeight="Normal"
41 ForegroundChecked="Orange"
42 ImageHeight="40"
43 ImageMargin="5"
44 ImageWidth="40"
45 Source="ms-appx:///Assets/Main/main_index_search_normal.png"
46 SourceChecked="ms-appx:///Assets/Main/main_index_search_pressed.png"
47 Style="{StaticResource RadioImageButtonStyle1}" />
48 <mycontrols:RadioImageButton
49 Grid.Column="3"
50 Margin="10"
51 Content="我的"
52 FontSize="12"
53 FontWeight="Normal"
54 ForegroundChecked="Orange"
55 ImageHeight="40"
56 ImageMargin="5"
57 ImageWidth="40"
58 Source="ms-appx:///Assets/Main/main_index_my_normal.png"
59 SourceChecked="ms-appx:///Assets/Main/main_index_my_pressed.png"
60 Style="{StaticResource RadioImageButtonStyle1}" />
61 </Grid>
转载请标明出处:http://www.cnblogs.com/xiaocaidev/p/6984375.html,本文出自:【xiaocaidev的博客】
UWP 自定义RadioButton实现Tab底部导航的更多相关文章
- UWP自定义RadioButton实现Tab底部导航
先看效果: 参照Android的实现方式用RadioButton来实现,但是Uwp的RadioButton并没有安卓的Selector选择器 下面是一个比较简单的实现,如果有同学有更好的实现,欢迎留言 ...
- Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏
在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示: 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...
- tab 切换 和 BottomNavigationBar 自定义 底部导航条
BottomNavigationBar 组件 BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold ...
- android应用开发--------------看RadioGroup源代码,写相似单选选项卡的集成控件(如底部导航,tab等等)
博客为 有时个哥 原创.如需转载请标明出处:http://blog.csdn.net/ls703/article/details/46694967 watermark/2/text/aHR0cDovL ...
- 二、Fragment+RadioButton实现底部导航栏
在App中经常看到这样的tab底部导航栏 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...
- 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化
效果: /** * Flutter BottomNavigationBar 自定义底部导航条.以及实现页面切换: * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...
- muse-ui底部导航自定义图标和字体颜色
最近在鼓捣用vue.js进行混合APP开发,遍寻许久终于找到muse-ui这款支持vue的轻量级UI框架,竟还支持按需引入,甚合萝卜意! 底部导航的点击波纹特效也是让我无比惊喜,然而自定义图标和字体颜 ...
- android开发(1):底部导航条的实现 | navigation tab | activity的创建
底部导航条,在iOS中叫tabbar,在android中叫bottombar或bottom navigation,是一个常用的切换页面的导航条. 同样,如果有良好的第三方库,我们应该优先考虑,能用好别 ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
随机推荐
- 在FL Studio中制作和优化人声和弦(Vocal Chords)
人声和弦在Future Bass.Melodic Dubstep等类型的电子音乐中被常用.与一般的和弦相同,其主要起到为主旋律做铺垫的效果,但是人声和弦加入了人声的因素,可以使得和弦更有趣,更有电子音 ...
- CLH lock queue的原理解释及Java实现
目录 背景 原理解释 Java代码实现 定义QNode 定义Lock接口 定义CLHLock 使用场景 运行代码 代码输出 代码解释 CLHLock的加锁.释放锁过程 第一个使用CLHLock的线程自 ...
- 分布式监控系统之Zabbix proxy
前文我们了解了zabbix 使用snmp和jmx信道采集数据的相关使用配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14029864.html:今天我们来 ...
- Nacos安装与启动教程
前言 Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计,Nacos基本上支持现在所有类型的服务,例如,Dubbo / gRPC服务,Spring Cloud R ...
- Linux 学习笔记02丨Linux 概述、常用快捷键、apt命令
Chapter 1. Linux 概述 Linux 是一种自由和开放源码的 Unix 操作系统, 是一个基于 POSIX 和 UNIX 的多用户.多任务.支持多线程和多CPU的操作系统.只要遵循 GN ...
- 腾讯云 CHDFS — 云端大数据存算分离的基石
随着网络性能提升,云端计算架构逐步向存算分离转变,AWS Aurora 率先在数据库领域实现了这个转变,大数据计算领域也迅速朝此方向演化. 存算分离在云端有明显优势,不但可以充分发挥弹性计算的灵活,同 ...
- 单体->集群->模块化->分布式微服务
开头语: 每篇一段开头语,在技术的道路中寻找文采的乐趣.(如果随笔中都是大白话勿喷,兄弟姐妹们) 单体项目 单体项目适用于小型开发,或自己来进行小项目的测试和使用. 单体项目的缺憾 多人开发项目所出现 ...
- JDK(JDK8,JDK11)高速下载
JDK(JDK8,JDK11)高速下载 oracl 需要登陆才能下载,网速还贼慢. 华为云各版本高速下载通道:https://repo.huaweicloud.com/java/jdk/
- 使用PyQt开发图形界面Python应用专栏目录
☞ ░ 前往老猿Python博文目录 ░ 本专栏为收费专栏的文章目录,对应的免费专栏为<PyQt入门知识目录>,两个专栏都为基于PyQt的Python图形界面开发基础教程,只是收费专栏中的 ...
- PyQt(Python+Qt)学习随笔:QTableWidget的takeItem和sortItems方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTableWidget中的takeItem方法从表格中取并去除项,sortItems方法对表格中的 ...