测试代码下载:http://files.cnblogs.com/djangochina/RadioButtonBug.zip

从上面列表选择不同的行,再设置下面不同的radiobutton看看结果

bug情况简单描述如下

一个列表控件listbox、listview或者datagrid,绑定如下

SelectedItem="{Binding SelectedData}"

当然这个SelectedData是一个实现了INotifyPropertyChanged的实体。比如说是Person,里面有 IsMan和IsWoman两个bool

绑定到两个RadionButton

<RadioButton IsChecked="{Binding IsMan}" Content="Man"/>
<RadioButton IsChecked="{Binding IsWoman}" Content="Woman"/>

当来回切换列表控件的选中项的时候会发现这两个RadioButton的值会有错误,会修改旧选中项属性的值。

解决方法

    public class RadioButtonExtended : RadioButton
{
public static readonly DependencyProperty IsCheckedExtProperty =
DependencyProperty.Register("IsCheckedExt", typeof(bool?), typeof(RadioButtonExtended),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Journal | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsCheckedRealChanged)); private static bool _isChanging; public RadioButtonExtended()
{
Checked += RadioButtonExtendedChecked;
Unchecked += RadioButtonExtendedUnchecked;
} public bool? IsCheckedExt
{
get
{
return (bool?)GetValue(IsCheckedExtProperty);
}
set
{
SetValue(IsCheckedExtProperty, value);
}
} public static void IsCheckedRealChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
_isChanging = true;
((RadioButtonExtended)d).IsChecked = (bool)e.NewValue;
_isChanging = false;
} private void RadioButtonExtendedChecked(object sender, RoutedEventArgs e)
{
if (!_isChanging)
IsCheckedExt = true;
} private void RadioButtonExtendedUnchecked(object sender, RoutedEventArgs e)
{
if (!_isChanging)
IsCheckedExt = false;
}
}

使用

<controls:RadioButtonExtended GroupName="Sex" IsCheckedExt="{Binding IsMan}" Content="Man"/>
<controls:RadioButtonExtended GroupName="Sex" IsCheckedExt="{Binding IsWoman}" Content="Woman"/>

wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值的更多相关文章

  1. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  2. WPF 分页控件 WPF 多线程 BackgroundWorker

    WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...

  3. 迟到的 WPF 学习 —— 控件

    这一章书中内容比较多而杂,但每个对象的内容又相对简短,所以只挑选里边有代表性的内容做记录. 1. Label 控件:一个基础的简单的 ContentControl,Labe 支持快捷键文本的设置,可以 ...

  4. WPF 截图控件之绘制方框与椭圆(四) 「仿微信」

    前言 接着上周写的截图控件继续更新 绘制方框与椭圆. 1.WPF实现截屏「仿微信」 2.WPF 实现截屏控件之移动(二)「仿微信」 3.WPF 截图控件之伸缩(三) 「仿微信」 正文 有开发者在B站反 ...

  5. WPF Popup 控件导致被遮挡内容不刷新的原因

    WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比 ...

  6. 创建 WPF 工具箱控件

    创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...

  7. WPF RichTextBox 控件常用方法和属性

    以下内容转自 http://blog.csdn.net/yulongguiziyao/article/details/25330551. 1. 取得已被选中的内容: (1)使用 RichTextBox ...

  8. RadioButton控件

    前台代码: <div> <asp:RadioButton ID="RadioButton1" runat="server" GroupName ...

  9. ASPxComboBox控件联动效果bug改进

    原文:ASPxComboBox控件联动效果bug改进 在应用第三方控件DevExpress控件的时候,大家应该对ASPxComboBox控件应该不是很陌生吧,尤其在做多级联动效果的时候,有着它独特的地 ...

随机推荐

  1. [ES6] 21. ESNext, ES6-Shim & Node

    ES-Next: Esnextis similar to traceur, you can use command line to compile files. Install: npm instal ...

  2. QT软键盘

    如何实现鼠标单击弹出软键盘 默认情况下,如果当前编辑框无焦点,则需要鼠标点击两次才弹出软键盘,其中第一次是让该编辑框获得焦点,第二次点击才弹出软键盘: 如果当前编辑框已经获得焦点,则点击一次就会弹出软 ...

  3. 什么是Cocos2d-x

    以下是官方对Cocos2d-x的说明."       Cocos2d-x is an open-source mobile 2D game framework, released under ...

  4. phpcms 源码分析二:

    这次是逆雪寒的common.inc.php第二部分: <?php /* 明天放假了.今天在写点罗.放假没空写了.要陪老婆,大家看了有什么不明白的.可以跟帖问.我懂的我会回答.谢谢 [/php] ...

  5. 自定义URL Scheme完全指南

    iPhone / iOS SDK 最酷的特性之一就是应用将其自身”绑定”到一个自定义 URL scheme 上,该 scheme 用于从浏览器或其他应用中启动本应用. 注册自定义 URL Scheme ...

  6. Cookie中用户登录信息的提示

    public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSe ...

  7. TCP/IP协议原理与应用笔记14:电路交换 和 分组交换

    1. 电路交换: (1)建立连接 (2)数据传输 (3)拆除连接 2. 分组交换 (1)数据报: 根据网络的特性,将数据报分成不同大小的部分,经过不同网路传递到相同的目的地.如下: 这里A--X  和 ...

  8. Java Interface and Abstraction

    本文引用资源申明: http://blog.csdn.net/xw13106209/article/details/6923556 http://www.cnblogs.com/dolphin0520 ...

  9. eclipse 手动安装皮肤

    关于自动使用eclipse 主题不成功的给出现在手动的安装方法和jar包 http://pan.baidu.com/s/1kVNEiYr http://pan.baidu.com/s/1cyTZrS ...

  10. Android AsyncTask 初探

    因为在开发Android应用时必须遵守单线程模型的原则: Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.在单线程模型中始终要记住两条法则: 1. 不要阻塞UI线程 2. 确 ...