效果如下图:
实现:
1.获取选中Item的内容
2.实现分组选择
3. 绑定 Item的图片和文字显示的定制显示
XMAL代码如下:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="Options demo" Height="450" Width="400">
<Grid>
<StackPanel Margin="10">
<StackPanel Margin="5">
<StackPanel Margin="5" >
<RadioButton x:Name="r1" Content="Varta" Checked="r_Checked"></RadioButton>
<RadioButton x:Name="r2" Content="Panasonic" Checked="r_Checked"></RadioButton>
<RadioButton x:Name="r3" Content="Others" Checked="r_Checked"></RadioButton>
</StackPanel>
<TextBox x:Name="txtMsg" Margin=" 2"></TextBox>
</StackPanel>
<Separator Height="5" Background="Green"/>
<StackPanel Margin="5" x:Name="sp2">
<Label>RadioButton 分组</Label>
<Label FontWeight="Bold">Are you ready?</Label>
<RadioButton GroupName="ready">Yes</RadioButton>
<RadioButton GroupName="ready">No</RadioButton>
<RadioButton GroupName="ready" IsChecked="True">Maybe</RadioButton>
<Label FontWeight="Bold">Male or female?</Label>
<RadioButton GroupName="sex">Male</RadioButton>
<RadioButton GroupName="sex">Female</RadioButton>
<RadioButton GroupName="sex" IsChecked="True">Not sure</RadioButton>
</StackPanel>
<StackPanel Margin="5" >
<Label FontWeight="Bold">Please Choose one?</Label>
<RadioButton>
<WrapPanel>
<Image Source="Images/yes.png" Width="16" Height="16" Margin="0,0,5,0" />
<TextBlock Text="Yes" Foreground="Green" />
</WrapPanel>
</RadioButton>
<RadioButton Margin="0,5">
<WrapPanel>
<Image Source="Images/no.png" Width="16" Height="16" Margin="0,0,5,0" />
<TextBlock Text="No" Foreground="Red" />
</WrapPanel>
</RadioButton>
<RadioButton IsChecked="True">
<WrapPanel>
<Image Source="Images/question.png" Width="16" Height="16" Margin="0,0,5,0" />
<TextBlock Text="Maybe" Foreground="Gray" />
</WrapPanel>
</RadioButton>
</StackPanel>
</StackPanel>
</Grid>
</Window>
后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void r_Checked(object sender, RoutedEventArgs e)
{
RadioButton RB = sender as RadioButton;
if (RB.IsChecked == true)
{
txtMsg.Text = RB.Content.ToString();
#region 选择
//switch (RB.Name)
//{
// case "r1":
// txtMsg.Text = "瓦尔塔";
// break;
// case "r2":
// txtMsg.Text = "松下";
// break;
// case "r3":
// txtMsg.Text = "其它";
// break;
//}
#endregion
}
}
}
}