`
clingingboy
  • 浏览: 26507 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

WPF and SL RadioButtonList Tip

阅读更多

      在以下情境下.使用数据绑定分离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与SL游戏动画教程ppt

    作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝色右手,WPF与SL游戏动画教程。作者:深蓝...

    Pro WPF and Silverlight MVVM-English

    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...

    WPF and Silverlight MVVM

    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 ...

    WPF制作不规则窗口 Tip消息提示框_可拖动_有关闭按钮.rar

    在本文中,我们将深入探讨如何在Windows Presentation Foundation (WPF) 中创建一个不规则形状的窗口,同时包含可拖动功能以及带有关闭按钮的Tip消息提示框。WPF是.NET Framework的一部分,它提供了一个强大的图形子...

    WPF(SL)下得Prism+MVVM框架(实用版直接用)

    **WPF(Windows Presentation Foundation)和Silverlight(SL)**是微软推出的两种不同的UI框架,它们主要用于构建桌面和网络应用程序。这两个平台都支持丰富的图形渲染和用户体验设计,但Silverlight主要针对Web...

    visifire for wpf/sl

    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 and Silverlight 学习笔记”精心制作成CHM格式,供各位WPF和Silverlight程序员学习使用。

    friendsofed foundation expression blend 2 building applications in wpf and silverlight

    friendsofed foundation expression blend 2 building applications in wpf and silverlightfriendsofed foundation expression blend 2 building applications in wpf and silverlight

    Professional.WPF.Programming

    From creating appealing graphics and animated structures to enhancing performance and security, you'll be programming in no time. &lt;br&gt;First you'll explore the WPF framework and learn how to ...

    Composite_Application_Guidance_for_WPF_and_Silverlight_-_October_2009

    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 Charters and Graphics Source Code

    《Apress Practical WPF Charts and Graphics Source Code》是关于Windows Presentation Foundation(WPF)图形和图表编程的一本书的配套源代码。WPF是.NET Framework的一部分,由Microsoft开发,用于构建丰富的、...

    Packt.MVVM.Survival.Guide.for.Enterprise.Architectures.in.Silverlight.And.WPF

    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,...

    Win32 Wrap WPF and MFC wrap WPF

    在本文档中,主要探讨了如何在Win32、MFC和WPF之间进行混合编程,以实现更丰富的用户界面和更好的应用程序体验。首先,我们来看看为何选择WPF。 **为什么选择WPF?** 1. **因为它是2008年**:WPF(Windows ...

    Good Book - Practical WPF Charts and Graphics

    《实用WPF图表与图形》是一本专注于Windows Presentation Foundation (WPF)图形系统和图表制作的专业书籍。WPF是微软开发的一种用于构建Windows桌面应用程序的框架,它为开发者提供了丰富的图形绘制、用户界面设计和...

    WPF鼠标拖放操作DragAndDrop

    在Windows Presentation Foundation (WPF) 中,鼠标拖放操作(DragAndDrop)是一种常见的用户交互方式,它允许用户通过鼠标将一个元素从一处移动到另一处。这种功能在各种应用程序中都有广泛的应用,例如文件管理器...

    WPF源码WPF 源码WPF 源码WPF 源码WPF 源码WPF 源码

    WPF(Windows Presentation Foundation)是微软.NET框架的一部分,用于构建丰富的、交互式的桌面应用程序。它提供了强大的图形渲染能力,支持2D和3D图形、动画、媒体集成、数据绑定、样式和模板等特性,极大地提升了...

Global site tag (gtag.js) - Google Analytics