先看效果:

参照Android的实现方式用RadioButton来实现,但是Uwp的RadioButton并没有安卓的Selector选择器

下面是一个比较简单的实现,如果有同学有更好的实现,欢迎留言,让我们共同进步。

1、首先自定义一个RadioImageButton控件,并定义几个依赖属性,代码如下

 using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; namespace Demo.UWP.Controls
{
public class RadioImageButton : RadioButton
{
//默认图片
public ImageSource Source
{
get { return (ImageSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
} // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(ImageSource), typeof(RadioImageButton), null); //选中图片
public ImageSource SourceChecked
{
get { return (ImageSource)GetValue(SourceCheckedProperty); }
set { SetValue(SourceCheckedProperty, value); }
} // Using a DependencyProperty as the backing store for SourceChecked. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SourceCheckedProperty =
DependencyProperty.Register("SourceChecked", typeof(ImageSource), typeof(RadioImageButton), null); //选中文字颜色
public SolidColorBrush ForegroundChecked
{
get { return (SolidColorBrush)GetValue(ForegroundCheckedProperty); }
set { SetValue(ForegroundCheckedProperty, value); }
} // Using a DependencyProperty as the backing store for ForegroundChecked. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ForegroundCheckedProperty =
DependencyProperty.Register("ForegroundChecked", typeof(SolidColorBrush), typeof(RadioImageButton), new PropertyMetadata(new SolidColorBrush(Colors.Black))); //图片宽度
public double ImageWidth
{
get { return (double)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
} // Using a DependencyProperty as the backing store for ImageWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageWidthProperty =
DependencyProperty.Register("ImageWidth", typeof(double), typeof(RadioImageButton), new PropertyMetadata()); public double ImageHeight
{
get { return (double)GetValue(ImageHeightProperty); }
set { SetValue(ImageHeightProperty, value); }
} // Using a DependencyProperty as the backing store for ImageHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageHeightProperty =
DependencyProperty.Register("ImageHeight", typeof(double), typeof(RadioImageButton), new PropertyMetadata()); public Thickness ImageMargin
{
get { return (Thickness)GetValue(ImageMarginProperty); }
set { SetValue(ImageMarginProperty, value); }
} // Using a DependencyProperty as the backing store for ImageMargin. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageMarginProperty =
DependencyProperty.Register("ImageMargin", typeof(Thickness), typeof(RadioImageButton), null); }
}

2、使用Blend工具生成RadioButton的模板,并修改其中的Grid,删除无用代码,添加一个Image控件,代码如下

 <ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Demo.UWP"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mycontrols="using:Demo.UWP.Controls"
mc:Ignorable="d">
<Style x:Key="RadioImageButtonStyle1" TargetType="mycontrols:RadioImageButton">
<Setter Property="Background" Value="{ThemeResource RadioButtonBackground}" />
<Setter Property="Foreground" Value="{ThemeResource RadioButtonForeground}" />
<Setter Property="BorderBrush" Value="{ThemeResource RadioButtonBorderBrush}" />
<Setter Property="Padding" Value="" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="MinWidth" Value="" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="FocusVisualMargin" Value="-7,-3,-7,-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="mycontrols:RadioImageButton">
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image
x:Name="ImageFront"
Width="{TemplateBinding ImageWidth}"
Height="{TemplateBinding ImageHeight}"
Margin="{TemplateBinding ImageMargin}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Source="{TemplateBinding Source}"
Stretch="Uniform" />
<ContentPresenter
x:Name="ContentPresenter"
Grid.Row=""
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}"
Foreground="{TemplateBinding Foreground}"
TextWrapping="Wrap" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<!--<Setter Target="ImageBack.Visibility" Value="Visible"/>
<Setter Target="ImageFront.Visibility" Value="Collapsed"/>-->
<Setter Target="ImageFront.Source" Value="{Binding SourceChecked, RelativeSource={RelativeSource TemplatedParent}}" />
<Setter Target="ContentPresenter.Foreground" Value="{Binding ForegroundChecked, RelativeSource={RelativeSource TemplatedParent}}" />
</VisualState.Setters>
<!--<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageBack" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="" Value="Visible" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageFront" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>-->
</VisualState>
<VisualState x:Name="Unchecked" />
<VisualState x:Name="Indeterminate" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

用VisualStateManager实现选中状态的实现,56-77行代码,这里Setter的Value并不能用TemplateBinding进行绑定,点击是会报一个Value的异常

3、下面就开始使用了,直接上代码

 <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<mycontrols:RadioImageButton
Grid.Row=""
Margin=""
Checked="RadioImageButton_Checked"
Content="首页"
FontSize=""
FontWeight="Normal"
ForegroundChecked="Orange"
ImageHeight=""
ImageMargin=""
ImageWidth=""
Source="ms-appx:///Assets/Main/main_index_home_normal.png"
SourceChecked="ms-appx:///Assets/Main/main_index_home_pressed.png"
Style="{StaticResource RadioImageButtonStyle1}" />
<mycontrols:RadioImageButton
Grid.Column=""
Margin=""
Content="品质优惠"
FontSize=""
FontWeight="Normal"
ForegroundChecked="Orange"
ImageHeight=""
ImageMargin=""
ImageWidth=""
Source="ms-appx:///Assets/Main/main_index_quality_normal.png"
SourceChecked="ms-appx:///Assets/Main/main_index_quality_pressed.png"
Style="{StaticResource RadioImageButtonStyle1}" />
<mycontrols:RadioImageButton
Grid.Column=""
Margin=""
Content="发现"
FontSize=""
FontWeight="Normal"
ForegroundChecked="Orange"
ImageHeight=""
ImageMargin=""
ImageWidth=""
Source="ms-appx:///Assets/Main/main_index_search_normal.png"
SourceChecked="ms-appx:///Assets/Main/main_index_search_pressed.png"
Style="{StaticResource RadioImageButtonStyle1}" />
<mycontrols:RadioImageButton
Grid.Column=""
Margin=""
Content="我的"
FontSize=""
FontWeight="Normal"
ForegroundChecked="Orange"
ImageHeight=""
ImageMargin=""
ImageWidth=""
Source="ms-appx:///Assets/Main/main_index_my_normal.png"
SourceChecked="ms-appx:///Assets/Main/main_index_my_pressed.png"
Style="{StaticResource RadioImageButtonStyle1}" />
</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. TCP三次握手四次挥手过程及各过程中客户端和服务器端的状态。

    #三次握手 客户端向服务器端发送SYN包,客户端进入SYN_SEND状态 服务器端收到客户端发送的包返回ACK+SYN包,服务器端进入SYN_RECV状态 客户端收到服务器端返回的包再发回ACK包,客 ...

  2. JQuery插件之Animate.css和 jquery-aniview

    Animate.css 一款强大的预设css3动画库 简介 animate.css 是一个来自国外的 CSS3 动画库,它预设了抖动(shake).闪烁(flash).弹跳(bounce).翻转(fl ...

  3. .NET跨平台实践:再谈用C#开发Linux守护进程

    Linux守护进程是Linux的后台服务进程,相当于Windows服务进程,对于为Linux开发服务程序的朋友来说,Linux守护进程相关技术是必不可少的,因为这个技术不仅仅是开发守护进程,还可以拓展 ...

  4. [Git]04 如何使用标签

     Git也可以对某一时间点上的版本打上标签.人们在发布某个软件版本(比如 v1.0 等等)的时候,经常这么做. 本节我们一起来学习如何如何新建标签,列出所有可用的标签,以及各种不同类型标签之间的差 ...

  5. sdkman安装

    软件开发工具管理包(Software Development Kit Manager,简称SDKMAN) 用来管理多个版本的开发环境的工具.提供命令行来安装.切换.删除.列出候选版本. 官网地址:ht ...

  6. vsftp使用方法与问题解决

    安装环境 OS:Centos 6.4 vsftp:vsftpd-2.2.2-11.el6_3.1.i686.rpm vsftpd配置文件:/etc/vsftpd/vsftpd.conf 一.      ...

  7. PHP学习笔记-2

    PHP 是一门弱类型语言: 在上面的实例中,我们注意到,不必向 PHP 声明该变量的数据类型.(跟Javascript很像啊!) PHP 会根据变量的值,自动把变量转换为正确的数据类型. 在强类型的编 ...

  8. poj2594最小顶点覆盖+传递闭包

    传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...

  9. 一个想法照进现实-《IT连》创业项目:创业时该不该用新手程序员

    前言: 距离上一篇文章,转眼已然一个多月了,这段时间没出来和大伙汇报创业的进度,怪我了. 最近又感冒了,已经一个多星期了,还在感冒中,不过感冒也不能偷懒了,每天都有大把的事情等着我解决~~~ 不过今天 ...

  10. 关于JS面向对象中原型和原型链以及他们之间的关系及this的详解

    一:原型和原型对象: 1.函数的原型prototype:函数才有prototype,prototype是一个对象,指向了当前构造函数的引用地址. 2.函数的原型对象__proto__:所有对象都有__ ...