WPF——自定义RadioButton

2024-09-03 07:36
文章标签 自定义 wpf radiobutton

本文主要是介绍WPF——自定义RadioButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求

需要做一组单选按钮,只要单选按钮的显示内容与需要匹配的内容一样,则该单选按钮就为选中状态,否则则为不选中状态;且需要将当前选中状态保存,后续再进入此页面时,匹配内容为此次的保存状态。

如下所示,3个单选按钮分别为Test1、Test2、Test3,需要匹配的内容为Test2。那么Test2就为选中状态,其它两个就为非选中状态。

深入分析

通过对需求的了解,可得出下述进一步需求:

单选按钮需要在选中状态下,需要将当前选中内容保存下来,也就是说需要将当前的选中内容作为匹配更新到VM中,在VM中再去实现相应的状态保存。

单选按钮在非选中状态下,可不将当前单选按钮的匹配内容清除(也可以清除),同时要保证这一组单选按钮有一个按钮是选中状态。

根据上述分析,从代码实现上需要考虑以下问题:

匹配内容需要绑定。

首次加载控件时,需要根据匹配内容设置当前控件是否为选中状态。

选中时需要将匹配内容更新,以便VM根据匹配内容的变化以保存它。

取消选中时,可考虑清空匹配内容,若要清空匹配匹配内容,那么在保存匹配内容时需要注意,不要将空值作为匹配内容的一个值保存;或者说空值不需要触发匹配内容的保存功能。

代码实现

按上述深入分析,那么仅需要自定义一个自定义单选按钮即可实现相应功能。详细代码如下:

    public class CustomRadio : RadioButton{static CustomRadio(){}public string Text{get { return (string)GetValue(TextProperty); }set { SetValue(TextProperty, value); }}public static readonly DependencyProperty TextProperty =DependencyProperty.Register("Text", typeof(string), typeof(CustomRadio), new PropertyMetadata(string.Empty, TextChanged));private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var radio = (CustomRadio)d;var ntxt = e.NewValue as string;// 获取Content属性的BindingExpression//var bindingExpression = BindingOperations.GetBindingExpression(radio, ContentControl.ContentProperty);//if (bindingExpression != null)//{//    // 强制更新绑定//    bindingExpression.UpdateTarget();//}// 现在可以获取到更新后的Content值var content = radio.Content?.ToString();if (content == ntxt){radio.IsChecked = true;}else{radio.IsChecked = false;}}protected override void OnChecked(RoutedEventArgs e){base.OnChecked(e);// 当RadioButton被选中时,更新Text属性Text = Content == null ? string.Empty : Content.ToString();}protected override void OnUnchecked(RoutedEventArgs e){base.OnUnchecked(e);// 当RadioButton未被选中时,清空Text属性Text = string.Empty;}}

注:上述代码中的Text依赖属性即是用于匹配内容的绑定。

以上为测试时使用的VM:

    internal partial class MainWindowViewModel : ObservableObject{[ObservableProperty]TestMethod testMethod = new TestMethod();[ObservableProperty]string testName = "Test2";[ObservableProperty]ObservableCollection<Student> students = [new(){Id=1,Name="test1",Age=12,Grade=1,Y=50},new(){Id=2,Name="test2",Age=12,Grade=2,Y=100},new(){Id=3,Name="test3",Age=12,Grade=3,Y=150},new(){Id=4,Name="test4",Age=12,Grade=4,Y=200},new(){Id=5,Name="test5",Age=12,Grade=5,Y=250},];Timer timer = new();public MainWindowViewModel(){timer.Start();timer.Interval = 2000;timer.Elapsed += Timer_Elapsed;}[RelayCommand]public void StopTimer(){timer.Stop();}private void Timer_Elapsed(object? sender, ElapsedEventArgs e){Random random = new();var index = random.Next(0, Students.Count);Students[index].Grade = random.Next(1, 101);var order = Students.OrderBy(e => e.Grade);//Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>//{}));int i = 0;foreach (Student student in order){student.Id = ++i;student.OldY = student.Y;student.Y = i * 50;student.IsUp = student.Y > student.OldY;//Students[i - 1] = student;}}}public partial class Student : ObservableObject{[ObservableProperty]private int id;[ObservableProperty]private string name = string.Empty;[ObservableProperty]private int age;[ObservableProperty]private int grade;[ObservableProperty]private int y;[ObservableProperty]private int oldY;[ObservableProperty]private bool isUp;}public partial class TestMethod : ObservableObject{[ObservableProperty]string one = "Test1";[ObservableProperty]string two = "Test2";[ObservableProperty]string three = "Test3";}

以下为测试时使用的WPF UI:

<Windowx:Class="WpfApp1.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:control="clr-namespace:WpfApp1.Control"xmlns:converter="clr-namespace:WpfApp1.Converter"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:WpfApp1"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:viewmodels="clr-namespace:WpfApp1.VM"x:Uid="Window1Title"Width="800"Height="450"mc:Ignorable="d"><Window.DataContext><viewmodels:MainWindowViewModel /></Window.DataContext><Window.Resources></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition Height="100" /><RowDefinition Height="*" /></Grid.RowDefinitions><StackPanel><control:CustomRadioContent="{Binding TestMethod.One}"GroupName="test1"Text="{Binding TestName, Mode=TwoWay}" /><control:CustomRadioContent="{Binding TestMethod.Two}"GroupName="test1"Text="{Binding TestName, Mode=TwoWay}" /><control:CustomRadioContent="{Binding TestMethod.Three}"GroupName="test1"Text="{Binding TestName, Mode=TwoWay}" /><Button Command="{Binding StopTimerCommand}" Content="Close" /></StackPanel></Grid>
</Window>

更优实现

然在搞定此自定义的RadioButton后,若只是单一的一行RadioButton,那么其实还可以有其它实现方式,比如用集合控件。

比如使用ListBox,这样还不用多次使用自定义的RadioButton,只需要绑定时将VM中的集合绑定于ListBox即可。

以下为ListBox结合CustomRadio的实现:

        <StackPanel><ListBox x:Name="list" ItemsSource="{Binding Methods}"><ListBox.ItemsPanel><ItemsPanelTemplate><StackPanel Orientation="Horizontal" /></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><DataTemplate><control:CustomRadioMargin="5"Content="{Binding}"GroupName="test1"Text="{Binding DataContext.TestName, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" /></DataTemplate></ListBox.ItemTemplate></ListBox><Button Command="{Binding StopTimerCommand}" Content="{x:Static loc:Resources.TextBlock1_TextBlock_Text}" /></StackPanel>

然,上述ListBox的实现并不是最好的,它存在以下问题:

每次ListBox内的单选按钮的选中与取消,会触发CustomRadio中的方法OnChecked(原按钮)与OnUnchecked(现按钮),这有两次调用才能将匹配内容更新;而只要改为ListBox的选中事件,那么只需要调用一次就可以更新匹配内容,也就是说从性能上来说,使用ListBox与CustomRadio的组合不是最优,最好是完全自定义ListBox来实现需求。

这篇关于WPF——自定义RadioButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132421

相关文章

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06