[玩一玩]WPF Tips: Uncheckable radio buttons

YapingXin 2012-08-20 06:47:19
加精
原文地址:http://blog.csdn.net/xinyaping/article/details/7887663

adio button 通常的行为是:某一组 Radio button 只能有一个被选中( Checked ),当一个被选中是其余的 Radio button 自动 Uncheck。

但是,有没有什么办法让这一组 Radio button 在被点击之后,再回复到任何一个 Radio button 都没有被选中的状态呢?比如说,两个 Radio button:Radio 1 和 Radio 2,初始状态下任何一个 radio button 都没有被选中,现在我们点击 Radio 1,则 Radio 1 被选中;再次点击 Radio 1,则 Radio 1 回复到没有被选中的状态,Radio 2 的状态也不受影响。能否实现这样的功能呢?网上搜不到现成的答案,但实际上是可以实现的。

实现的关键是利用 RadioButton 的 PreviewMouseLeftButtonDown 事件,该事件能够获得 RadioButton 被点击时,它之前的状态。有了这个状态我们就可以判断出这个 RadioButton 应该从未选中的状态置为选中的状态,还是从被选中的状态置为未选中的状态。


我写了个小例子,可从此处下载源代码和编译好之后的可执行文件(基于.Net 3.5)
http://download.csdn.net/detail/xinyaping/4513569

源代码如下:

MainWindow.xaml


<Window x:Class="RadioButttonUnCheckDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="300" Height="95" WindowStyle="ToolWindow"
Title="Demo: Uncheckable radio buttons">

<Grid Height="24" Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>

<Label Grid.Column="0" VerticalAlignment="Center">Please select: </Label>

<WrapPanel Grid.Column="1">
<RadioButton Name="radio1" Content="1" Margin="5,0,5,0" GroupName="GroupName1"
PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"
Checked="RadioButton_StateChanged"
Unchecked="RadioButton_StateChanged" />
<RadioButton Name="radio2" Content="2" Margin="5,0,5,0" GroupName="GroupName1"
PreviewMouseLeftButtonDown="RadioButton_PreviewMouseLeftButtonDown"
Checked="RadioButton_StateChanged"
Unchecked="RadioButton_StateChanged" />
</WrapPanel>

<Label Grid.Column="2" VerticalAlignment="Center" Name="labelResult" />
</Grid>
</Window>


MainWindow.xaml.cs


namespace RadioButttonUnCheckDemo
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
}

/// <summary>
/// Gets a value indicating whether RadioButton radio1 is checked.
/// </summary>
/// <value>
/// <c>true</c> if RadioButton radio1 is radio1 checked; otherwise, <c>false</c>.
/// </value>
private bool IsRadio1Checked
{
get { return this.radio1.IsChecked == null ? false : (bool)this.radio1.IsChecked; }
}

/// <summary>
/// Gets a value indicating whether RadioButton radio2 is checked.
/// </summary>
/// <value>
/// <c>true</c> if RadioButton radio2 is radio1 checked; otherwise, <c>false</c>.
/// </value>
private bool IsRadio2Checked
{
get { return this.radio2.IsChecked == null ? false : (bool)this.radio2.IsChecked; }
}

/// <summary>
/// Handles the PreviewMouseLeftButtonDown event of the RadioButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void RadioButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender != null && sender is RadioButton)
{
RadioButton radioButton = sender as RadioButton;
bool isChecked =
radioButton.IsChecked == null ?
false :
(bool)radioButton.IsChecked;

if (isChecked)
{
radioButton.IsChecked = false;
e.Handled = true;
}
}
}

/// <summary>
/// Handles the StateChanged event of the RadioButton control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private void RadioButton_StateChanged(object sender, RoutedEventArgs e)
{
this.SetResult();
}

/// <summary>
/// Sets the result.
/// </summary>
private void SetResult()
{
if (!this.IsRadio1Checked && !this.IsRadio2Checked)
{
this.labelResult.Content = "Selected: N/A";
}
else if (this.IsRadio1Checked)
{
this.labelResult.Content = "Selected: Radio 1";
}
else if (this.IsRadio2Checked)
{
this.labelResult.Content = "Selected: Radio 2";
}
}
}
}


可从此处下载源代码和编译好之后的可执行文件(基于.Net 3.5)
http://download.csdn.net/detail/xinyaping/4513569
...全文
1164 46 打赏 收藏 转发到动态 举报
写回复
用AI写文章
46 条回复
切换为时间正序
请发表友善的回复…
发表回复
xupe95518 2012-08-30
  • 打赏
  • 举报
回复
不好意思,回帖赚积分。
风骑士之怒 2012-08-30
  • 打赏
  • 举报
回复
支持LZ分享。。。
YapingXin 2012-08-30
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 的回复:]
cab架构?
[/Quote]

MVP
xia_alngf 2012-08-30
  • 打赏
  • 举报
回复
cab架构?

[Quote=引用 33 楼 的回复:]

总之,MVVM我会多做尝试。但是目前我手头有一个项目是基于MVP模式的,呵呵。多谢30楼的解答。
[/Quote]
zhucheng22 2012-08-28
  • 打赏
  • 举报
回复
看不懂啊
xxxcloud 2012-08-28
  • 打赏
  • 举报
回复
看不懂,不过先顶下!
看着捉急 2012-08-22
  • 打赏
  • 举报
回复
洒家也看不懂哎
YapingXin 2012-08-21
  • 打赏
  • 举报
回复
但是11楼,真的没感觉到您这么写有什么好处~~而且你程序里还有不小的bug...
YapingXin 2012-08-21
  • 打赏
  • 举报
回复
非常感谢13楼,让我很有启发!
gytxwjh 2012-08-21
  • 打赏
  • 举报
回复
看不懂,不过先顶下!
快溜 2012-08-21
  • 打赏
  • 举报
回复
野比最近咋不出作品了?晚上睡觉都不踏实。
cnlbfs 2012-08-21
  • 打赏
  • 举报
回复
不错不错啊。
YapingXin 2012-08-21
  • 打赏
  • 举报
回复
总之,MVVM我会多做尝试。但是目前我手头有一个项目是基于MVP模式的,呵呵。多谢30楼的解答。
SocketUpEx 2012-08-21
  • 打赏
  • 举报
回复
public class RadioButtonEx : RadioButton
{
protected override void OnClick(EventArgs e)
{
if (this.Checked)
{
this.Checked = false;
}
else
{
base.OnClick(e);
}
}
}


在哪写不是写
WinForm就一个写代码的地
没争论

RexZheng 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 的回复:]

引用 27 楼 的回复:

引用 25 楼 的回复:
眼镜哥15年的开发经验,年薪30~~40万上落
被你说成阅读量不够~~~~


我可以把你这段话视为对我的变相的讽刺吗?我真诚地认为你没必要这么做。界面编程的经验我确实比较少。如果你可以把你的语言化为正面的批评,那么我接受你的批评。


这哪是讽刺你啊
我明显是讽刺23楼
眼镜哥绝对是我们菜鸟仰望+努力的方向啊
……
[/Quote]

不怎么来CSDN了,很多人不怎么认识,仅就代码论代码
RexZheng 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 的回复:]

引用 22 楼 的回复:
如果你能从界面功能和业务逻辑分开的角度考察问题,你才能体会这样写的好处。
WPF的很多优势需要你在使用了MVVM之后才能发现,Behavior是对MVVM的一个小小补充,在你深入WPF之后,你会发现你很少会在XAML中写事件。


是,如果用MVVM,确实非常方便。所以我对#13楼的实现方法打算加以吸收采纳。可是你#11楼的做法呢,既没沾上MVVM的光,也没……
[/Quote]

MVVM在实践中会有所缺陷,有一部分问题解决不了,一般是用Behavior的形式解决。
况且这个问题完全可以理解为纯界面问题,如非特别需要,一般不需要“侵入”VM中的代码。
YapingXin 2012-08-21
  • 打赏
  • 举报
回复

MVVM 是非常棒的界面编程设计模式,很多情况下我们都希望用它,但它也不是唯一的设计模式。虽然我界面编程经验并不多,但你也不能说别人没有用MVVM那么别人就是菜鸟,写的代码就不高效。

所有的架构模式的目的都是合理地解耦,使代码可测试。MVC、MVP、MVVM,在实际中都有它自己的适用场景。
SocketUpEx 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 的回复:]

引用 25 楼 的回复:
眼镜哥15年的开发经验,年薪30~~40万上落
被你说成阅读量不够~~~~


我可以把你这段话视为对我的变相的讽刺吗?我真诚地认为你没必要这么做。界面编程的经验我确实比较少。如果你可以把你的语言化为正面的批评,那么我接受你的批评。
[/Quote]

这哪是讽刺你啊
我明显是讽刺23楼
眼镜哥绝对是我们菜鸟仰望+努力的方向啊
代码嘛,我先看看
一会再说


bbsphixy 2012-08-21
  • 打赏
  • 举报
回复
太深奥了!~~
YapingXin 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 的回复:]
眼镜哥15年的开发经验,年薪30~~40万上落
被你说成阅读量不够~~~~
[/Quote]

我可以把你这段话视为对我的变相的讽刺吗?我真诚地认为你没必要这么做。界面编程的经验我确实比较少。如果你可以把你的语言化为正面的批评,那么我接受你的批评。

加载更多回复(16)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧