在以下情境下.使用数据绑定分离UI与后端Model,有两个RadioButton,用于选择True or False(如果用CheckBox则就没这么多复杂的问题了).
实现步骤如下,
(1)用ListBox定义一个RadioButton模板
<!--for RadioButton ListBox--> <Style x:Key="HorizontalRadioButtonListStyle" TargetType="ListBox"> <Style.Resources> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Grid Margin="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <RadioButton IsChecked="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" /> <ContentPresenter Grid.Column="1" Margin="2,0,0,0" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Style.Resources> <Setter Property="ItemsPanel"> <Setter.Value> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </Setter.Value> </Setter> <Setter Property="BorderThickness" Value="0" /> <Setter Property="Background" Value="Transparent" /> </Style>
(2)定义类型转换器(以传入的参数进行判定)
public class MulitBooleanConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int serviceParameter =int.Parse(parameter.ToString()); bool isOnservice=(bool)value; bool result = false; switch (serviceParameter) { case 0: result = true && isOnservice; break; case 1: result = !isOnservice; break; } return result; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { int serviceParameter = int.Parse(parameter.ToString()); bool isOnservice = (bool)value; bool result = false; switch (serviceParameter) { case 0: result = true && isOnservice; break; case 1: result = false && isOnservice; break; } return value; } #endregion }
(3)使用如下(同时双向绑定一个属性xxx)
<ListBox Style="{StaticResource HorizontalRadioButtonListStyle}"> <ListBox.Items> <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=0}">在线</ListBoxItem> <ListBoxItem IsSelected="{Binding xxx,Converter={StaticResource boolConverter},ConverterParameter=1}">离线</ListBoxItem> </ListBox.Items> </ListBox>
发表评论
-
wpf开发常见问题(1)
2008-10-22 21:42 1600经过一段时间wpf的学习和实际开发.现在与大家分享 ... -
初始化集合项依赖属性
2008-11-03 13:38 822在wpf自定义属性时,若此属性为集合类型的话,如 ... -
wpf datagrid笔记 part 1
2008-12-10 23:35 29181,基本属性 开关基本 ... -
Prism V2之旅(2)
2009-01-07 10:02 1182本篇将介绍Prism中Region的使用. 本篇D ... -
Prism V2之旅(3)
2009-01-07 15:24 865上篇介绍了Region的基本应用,这篇接上篇,继续介绍 ... -
Prism V2之旅(4)
2009-01-07 23:42 974上篇介绍了RegionAdapter的作用及如何如何 ... -
Prism V2之旅(5)
2009-01-13 11:54 1110上篇介绍了WPF的Attach Behavior(附加行 ... -
Prism V2之旅(6)
2009-01-13 15:14 1040这篇来讲事件.事件主要用来交互. 监听事件 我订 ... -
prism v2之旅(7)
2009-01-15 23:53 1145这篇继续介绍Module的功能. 第5篇已经介绍过模块的 ... -
XAML Guidelines
2009-01-17 11:31 860http://blogs.msdn.com/jaimer ... -
wpf中xaml的类型转换器与标记扩展
2009-02-02 14:03 2347这篇来讲wpf控件属性的类型转换器 类型转换器 类 ... -
wpf控件设计时支持(3)
2009-02-02 14:12 2780wpf设计时调试 编辑模型 装饰器 1.wpf ... -
wpf控件样式管理示意图
2009-02-04 14:40 1663...
相关推荐
作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝...
WPF and Silverlight are unlike any other user interface (UI) technologies. They have been built to a new paradigm that―if harnessed correctly―can yield unprecedented power and performance. This book...
This book was conceived from a need to explain the MVVM pattern and how it helps structure WPF and Silverlight applications. I had worked on a number of projects where these technologies were used ...
在本文中,我们将深入探讨如何在Windows Presentation Foundation (WPF) 中创建一个不规则形状的窗口,同时包含可拖动功能以及带有关闭按钮的Tip消息提示框。WPF是.NET Framework的一部分,它提供了一个强大的图形子...
**WPF(Windows Presentation Foundation)和Silverlight(SL)**是微软推出的两种不同的UI框架,它们主要用于构建桌面和网络应用程序。这两个平台都支持丰富的图形渲染和用户体验设计,但Silverlight主要针对Web...
visifire图表控件,支持wpf和silverlight Visifire.Charts.dll Visifire.Commons.dll 另,Visifire服务器端技术,能与ASP,ASP Net, SharePoint,PHP, JSP, ColdFusion, Python, Ruby或者HTML一起使用。
将网上流传甚广的“WPF and Silverlight 学习笔记”精心制作成CHM格式,供各位WPF和Silverlight程序员学习使用。
Eliminate unnecessary code by taking advantage of the MVVM pattern in Silverlight and WPF using this book and eBook - less code, fewer bugs Build an enterprise application using Silverlight and WPF,...
在本系列的学习笔记中,我们将深入探讨Windows Presentation Foundation (WPF) 和 Silverlight这两个由Microsoft推出的用户界面框架。这两者都是用于构建丰富的桌面和Web应用程序的技术,但它们各有特点和适用场景。...
friendsofed foundation expression blend 2 building applications in wpf and silverlightfriendsofed foundation expression blend 2 building applications in wpf and silverlight
From creating appealing graphics and animated structures to enhancing performance and security, you'll be programming in no time. <br>First you'll explore the WPF framework and learn how to ...
The Composite Application Guidance for WPF and Silverlight is a set of guidance designed to help you more easily manage the complexities you may face when building enterprise-level Windows ...
《Apress Practical WPF Charts and Graphics Source Code》是关于Windows Presentation Foundation(WPF)图形和图表编程的一本书的配套源代码。WPF是.NET Framework的一部分,由Microsoft开发,用于构建丰富的、...
在本文档中,主要探讨了如何在Win32、MFC和WPF之间进行混合编程,以实现更丰富的用户界面和更好的应用程序体验。首先,我们来看看为何选择WPF。 **为什么选择WPF?** 1. **因为它是2008年**:WPF(Windows ...
《实用WPF图表与图形》是一本专注于Windows Presentation Foundation (WPF)图形系统和图表制作的专业书籍。WPF是微软开发的一种用于构建Windows桌面应用程序的框架,它为开发者提供了丰富的图形绘制、用户界面设计和...
WPF(Windows Presentation Foundation)是微软.NET框架的一部分,用于构建丰富的、交互式的桌面应用程序。它提供了强大的图形渲染能力,支持2D和3D图形、动画、媒体集成、数据绑定、样式和模板等特性,极大地提升了...
点芝麻笔记2.0是一款基于WPF(Windows Presentation Foundation)技术构建的富文本编辑器,其源码的分享为开发者提供了一个深入理解WPF应用程序设计和优化的宝贵资源。相较于前一版本1.0,这款编辑器在重构过程中...