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,如需转载请自行联系原作者