先看效果:

参照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. Extjs6(一)——用sencha cmd建立一个ExtJs小项目

    本文基于ext-6.0.0 一.用sencha cmd建立一个ExtJs小项目 首先,需要一个命令行工具.进入extjs所在目录. 然后,输入:sencha -sdk [ExtJs6.0文件夹地址] ...

  2. MongoDB基础教程系列--第九篇 MongoDB 分片

    1.分片介绍 分片(sharding)是将数据拆分,将其分散存到不同机器上的过程.MongoDB 支持自动分片,可以使数据库架构对应用程序不可见.对于应用程序来说,好像始终在使用一个单机的 Mongo ...

  3. [.NET] 《C# 高效编程》(一) - C# 语言习惯

    C# 语言习惯 目录 一.使用属性而不是可访问的数据成员 二.使用运行时常量(readonly)而不是编译时常量(const) 三.推荐使用 is 或 as 操作符而不是强制类型转换 四.使用 Con ...

  4. TagHelper的一些个人学习体会(发现了微软官方文档的一个错误)

    最近一直在学习.net core 上周六开始学习Taghelper的自定义,准备周日写个博客和大家分享一下学习体会,无奈周日去考科四了,回来之后就感冒了,现在还没好.可是我发现了微软官方文档的一个错误 ...

  5. Virtualbox mac版centos7.2 安装共享文件夹

    因为用的是mac本,在自己电脑上用virtualbox安装了centos7.2 虚拟机,为了实现与本机交换文件,需要用到贡享文件夹功能. 网上搜的时候多是windows版的,所以自己写一个mac版的. ...

  6. [Day03] 循环语句、list相关练习题

    用户输入两个数,求平均值. 让用户一直输入数字,如果输入的是'0',终止程序打印所有数字之和. 让用户一直输入数字(只输入数字),如果没输入任何值,终止程序打印所有输入数字的平均值. 求出这个list ...

  7. crontab定时任务不执行的原因

    1.重启crontab若是遇见"You (cloudlogin) are not allowed to use this program (crontab)                 ...

  8. PHP学习笔记-2

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

  9. es6英文文档翻译

    ECMA-262英文文档翻译,github地址: https://github.com/zhoushengmufc/es6 ECMA-262英文文档翻译,在线地址: http://zhoushengf ...

  10. 使用Spire.Doc组件利用模板导出Word文档

    以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...