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/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
浙公网安备 33010602011771号