WPF 怎样实现单选列表控件?

WPF没有像ASP.NET提供一个RadioButtonList的控件。幸运的是,我们可以利用WPF样式和模板的强大功能,用纯粹的XAML代码实现这个功能

<Window x:Class= "ControlTest2.ListBoxTest2"
     xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:s= "clr-namespace:System;assembly=mscorlib" >
     <Window.Resources>
         <x:Array Type= "{x:Type s:String}"  x:Key= "data" >
             <s:String>Option1</s:String>
             <s:String>Option2</s:String>
             <s:String>Option3</s:String>
         </x:Array>
     </Window.Resources>
     <StackPanel DataContext= "{StaticResource data}" >
       <TextBlock Margin= "5" >
        <TextBlock Text= "Current Option:" />
        <TextBlock Text= "{Binding /}" />
     </TextBlock>
       <ListBox
        ItemsSource= "{Binding}"
        IsSynchronizedWithCurrentItem= "True"
        Width= "240"
        Height= "60"
        HorizontalAlignment= "Left" >
             <ListBox.ItemContainerStyle>
                 <Style TargetType= "{x:Type ListBoxItem}" >
                     <Setter Property= "Template" >
                         <Setter.Value>
                             <ControlTemplate TargetType= "{x:Type ListBoxItem}" >
                                 <RadioButton
                                     IsChecked= "{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}"
                                     Content= "{TemplateBinding Content}" />
                             </ControlTemplate>
                         </Setter.Value>
                     </Setter>
                 </Style>
             </ListBox.ItemContainerStyle>
         </ListBox>
         <!--这种方法跟简单-->
          <StackPanel>
             <RadioButton GroupName= "x" >Radio1</RadioButton>
            <RadioButton GroupName= "x" >Radio2</RadioButton>
            <RadioButton GroupName= "x" >Radio3</RadioButton>
         </StackPanel>
     </StackPanel>  
</Window>

  参考:http://social.msdn.microsoft.com/Forums/zh-CN/wpfzhchs/thread/857fdaa9-5c67-4e0a-a1fd-037f72577c76



本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/11/14/2248381.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值