基本信息
源码名称:wpf 多个button 选中效果界面Demo
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2016-02-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public string Title { get { return title.Text; } }

        public string SelectedValue { get; private set; }

        public event RoutedEventHandler CheckChanged;

        public MainWindow()
        {
            InitializeComponent();
            kkkk("radiobutton模版设计成button样式,赵保军qq:1060354472 博客地址:http://blog.csdn.net/rrzhaobaojun", new string[] { "更早", "2006", "2007", "2008", "2009", "2010", "2011" });
        }
        private void kkkk(string name, IEnumerable<string> values)
    {
        
        this.title.Text = name;

        // 添加“全部”选择项
        var c = CreateItem("全部");
        c.IsChecked = true;
        this.wrap.Children.Add(c);
        this.SelectedValue = c.Content as string;

        // 添加一般选择项
        foreach(var i in values)
            this.wrap.Children.Add(CreateItem(i));
    }

    private void WhenCheckChanged(object sender, RoutedEventArgs e)
    {
        var c = sender as RadioButton;
        var isChecked = c.IsChecked ?? false;
        // 依据IsChecked属性值选择模板
        c.Template = Resources[isChecked ? "Checked": "UnChecked"] as ControlTemplate;

        if(isChecked)
        {
            this.SelectedValue = c.Content as string;
            // 触发CheckChanged事件,以便外部代码访问
            if(CheckChanged != null)
                CheckChanged(this, null);
        }
    }

    // 创建新的单选项
    private RadioButton CreateItem(string item)
    {
        var radio = new RadioButton();
        radio.Content = item;
        // 设定缺省的UnChecked模板
        radio.Template = this.Resources["UnChecked"] as ControlTemplate;
        // 订阅事件
        radio.Checked  = WhenCheckChanged;
        radio.Unchecked  = WhenCheckChanged;
        return radio;
    }
    }
}