先看效果:

参照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. Web 网站服务

    Apache 简介 Apache HTTP Server(简称Apache)是开放源码的网页服务器,基于标准的HTTP网站协议提供网页浏览服务,在Web服务器领域中长期保持着超过半数的份额.Apach ...

  2. Linux基础(6)

    Linux基础(六) shell脚本中的三大循环和函数知识点 一.流程控制之if结构 1.简单的if实例: #!/bin/bash var='/etc/init.d' #var='/dev/sda' ...

  3. Python-3------新年考试周的Python学习

    2016一开始就是考试周,准备专业课的考试复习.每天上午复习,晚上复习到8点半,之后到10点这点时间来看Python.庆幸没有在忙碌的时候荒废 Python的学习. 期待寒假,以前寒假在家总是没事做, ...

  4. 新型钓鱼手段预警:你看到的 аррӏе.com 真是苹果官网?

    研究人员发现一种"几乎无法检测"的新型钓鱼攻击,就连最细心的网民也难以辨别.黑客可通过利用已知漏洞在 Chrome.Firefox 与 Opera 浏览器中伪造显示合法网站域名(例 ...

  5. 七牛整合 ueditor (拦住那头牛,七牛又如何)

    最近遇到个项目,要求所有图片都必须整合到七牛上,看了把你谈文档踩在前辈们的基础上终于把他完成了,恰巧本屌丝最近刚好有时间,本着天下屌丝是一家的原则,和小朋友们一同学习 闲话少说入正题. 第一 :下载编 ...

  6. STL容器之优先队列(转)

    STL容器之优先队列 原地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 优先级队列,以前刷题的时候用的比较熟,现在竟然我只能记得它的关 ...

  7. poj1410计算几何线段相交

    You are to write a program that has to decide whether a given line segment intersects a given rectan ...

  8. String,StringBuilder,StringBuffer的对比测试

    public class TestString { private static final int COUNT = 10000000; public static void main(String ...

  9. RabbitMQ集群和失败处理

    RabbitMQ内建集群的设计用于完成两个目标:允许消费者和生产者在RabbitMQ节点在奔溃的情况下继续运行,以及通过添加更多的节点来线性扩展消息通信的吞吐量.当失去一个RabbitMQ节点时客户端 ...

  10. 龟兔赛跑,杭电oj-2059

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2059 [Problem Description] 据说在很久很久以前,可怜的兔子经历了人生中最大的打击 ...