先看效果:

参照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底部导航的更多相关文章

  1. UWP自定义RadioButton实现Tab底部导航

    先看效果: 参照Android的实现方式用RadioButton来实现,但是Uwp的RadioButton并没有安卓的Selector选择器 下面是一个比较简单的实现,如果有同学有更好的实现,欢迎留言 ...

  2. Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏

    在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示:   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...

  3. tab 切换 和 BottomNavigationBar 自定义 底部导航条

    BottomNavigationBar 组件    BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold ...

  4. android应用开发--------------看RadioGroup源代码,写相似单选选项卡的集成控件(如底部导航,tab等等)

    博客为 有时个哥 原创.如需转载请标明出处:http://blog.csdn.net/ls703/article/details/46694967 watermark/2/text/aHR0cDovL ...

  5. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  6. 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化

    效果: /**  * Flutter  BottomNavigationBar 自定义底部导航条.以及实现页面切换:  * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...

  7. muse-ui底部导航自定义图标和字体颜色

    最近在鼓捣用vue.js进行混合APP开发,遍寻许久终于找到muse-ui这款支持vue的轻量级UI框架,竟还支持按需引入,甚合萝卜意! 底部导航的点击波纹特效也是让我无比惊喜,然而自定义图标和字体颜 ...

  8. android开发(1):底部导航条的实现 | navigation tab | activity的创建

    底部导航条,在iOS中叫tabbar,在android中叫bottombar或bottom navigation,是一个常用的切换页面的导航条. 同样,如果有良好的第三方库,我们应该优先考虑,能用好别 ...

  9. 微信小程序-自定义底部导航

    代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

随机推荐

  1. Guitar Pro吉他指弹入门——双手泛音

    曾经有一段时间在琴行里经常遇到有人来试琴,很多人试弹得曲子就是郑成河的<Flaming>,直译过来就是热情的意思.这首曲子里面有很多泛音存在,吉他泛音类似于钟鸣或者摇铃的声音,是一种令人耳 ...

  2. Httprunner初步学习

    一:简介 一直在技术博客上看到Httprunner测试框架,但始终不太明白这个框架的具体作用,今天就花点时间来初步学习了解一下. HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架, ...

  3. docker提示容器已存在

    docker ps -a docker rm 容器id 重启启动

  4. JUC详解--【Foam番茄】

    1.什么是JUC java.util 工具包 业务:普通的线程代码 Thread Runnable 没有返回值,效率相比于 Callable 相对较低! 2.线程和进程 进程:一个程序,QQ.exe ...

  5. asp.net core 集成 Prometheus

    asp.net core 集成 prometheus Intro Prometheus 是一个开源的现代化,云原生的系统监控框架,并且可以轻松的集成 PushGateway, AlertManager ...

  6. 2020年团体程序设计天梯赛-总决赛 L2-4 网红点打卡攻略

    题目:一个旅游景点,如果被带火了的话,就被称为"网红点".大家来网红点游玩,俗称"打卡".在各个网红点打卡的快(省)乐(钱)方法称为"攻略" ...

  7. MindSpore手写数字识别初体验,深度学习也没那么神秘嘛

    摘要:想了解深度学习却又无从下手,不如从手写数字识别模型训练开始吧! 深度学习作为机器学习分支之一,应用日益广泛.语音识别.自动机器翻译.即时视觉翻译.刷脸支付.人脸考勤--不知不觉,深度学习已经渗入 ...

  8. LaTeX相关自学文档

    install-latex-guide-zh-cn: lshort-zh-cn: 百度网盘链接:https://pan.baidu.com/s/1cBv9Fu8KFaf0QFZ7_slxmw 提取码: ...

  9. CentOS下配置VNC

    配置桌面 # 安装gnome桌面环境 yum groupinstall Desktop -y # 安装中文语言支持包(可选) yum groupinstall 'Chinese Support' -y ...

  10. 少标签数据学习:宾夕法尼亚大学Learning with Few Labeled Data

    目录 Few-shot image classification Three regimes of image classification Problem formulation A flavor ...