Normalement, nous utilisons RadioButtons pour présenter un type de données Enum à l'utilisateur. Et ce que nous faisons habituellement est d'utiliser un ItemsControl pour présenter un groupe de RadioButtons, avec chacun lié à un ViewModel. Ci-dessous un exemple d'application que je viens d'écrire qui montre comment RadioButtons peut être utilisé de deux façons: la première est une approche directe (et cela peut répondre à vos questions ci-dessus) et la seconde utilise une approche MVVM.
BTW, c'est juste quelque chose que j'ai écrit rapidement (ouais, j'ai beaucoup de temps dans mes mains) donc je ne dirai pas que tout ici est la façon parfaite de faire les choses. Mais j'espère que vous trouverez ce utile:
XAML:
<Window x:Class="RadioButtonSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RadioButtonSample"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel x:Name="sp"/>
<Button x:Name="showChoice" Click="showChoice_Click">Show Choice</Button>
<StackPanel x:Name="sp2">
<StackPanel.DataContext>
<local:ViewModel/>
</StackPanel.DataContext>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding Path=Choices}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton IsChecked="{Binding Path=IsChecked}" Content="{Binding Path=Choice}" GroupName="ChoicesGroup"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button x:Name="showChoice2" Click="showChoice2_Click">Show Choice2</Button>
</StackPanel>
</StackPanel>
code-behind:
using System;
using System.Collections;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
using System.Collections.Generic;
namespace RadioButtonSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Initialize the first group of radio buttons and add them to the panel.
foreach (object obj in Enum.GetValues(typeof(ChoicesEnum)))
{
RadioButton rb = new RadioButton() { Content = obj, };
sp.Children.Add(rb);
rb.Checked += new RoutedEventHandler(rb_Checked);
rb.Unchecked += new RoutedEventHandler(rb_Unchecked);
}
}
void rb_Unchecked(object sender, RoutedEventArgs e)
{
Console.Write((sender as RadioButton).Content.ToString() + " checked.");
}
void rb_Checked(object sender, RoutedEventArgs e)
{
Console.Write((sender as RadioButton).Content.ToString() + " unchecked.");
}
private void showChoice_Click(object sender, RoutedEventArgs e)
{
foreach (RadioButton rb in sp.Children)
{
if (rb.IsChecked == true)
{
MessageBox.Show(rb.Content.ToString());
break;
}
}
}
private void showChoice2_Click(object sender, RoutedEventArgs e)
{
//Show selected choice in the ViewModel.
ChoiceVM selected = (sp2.DataContext as ViewModel).SelectedChoiceVM;
if (selected != null)
MessageBox.Show(selected.Choice.ToString());
}
}
//Test Enum
public enum ChoicesEnum
{
Choice1,
Choice2,
Choice3,
}
//ViewModel for a single Choice
public class ChoiceVM : INotifyPropertyChanged
{
public ChoicesEnum Choice { get; private set; }
public ChoiceVM(ChoicesEnum choice)
{
this.Choice = choice;
}
private bool isChecked;
public bool IsChecked
{
get { return this.isChecked; }
set
{
this.isChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
}
//Sample ViewModel containing a list of choices
//and exposes a property showing the currently selected choice
public class ViewModel : INotifyPropertyChanged
{
public List<ChoiceVM> Choices { get; private set; }
public ViewModel()
{
this.Choices = new List<ChoiceVM>();
//wrap each choice in a ChoiceVM and add it to the list.
foreach (var choice in Enum.GetValues(typeof(ChoicesEnum)))
this.Choices.Add(new ChoiceVM((ChoicesEnum)choice));
}
public ChoiceVM SelectedChoiceVM
{
get
{
ChoiceVM selectedChoice = this.Choices.FirstOrDefault((c) => c.IsChecked == true);
return selectedChoice;
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#endregion
}
}
merci - dans mon cas, je sais pas le nombre de boutons radio à la compilation, donc je ne suis pas sûr de l'approche enum - des conseils sur ce qu'il faut faire dans ce cas? – Greg
Je crois que cela fonctionnera de la même manière. Je pense que les éléments de votre groupe de RadioButtons seront une sorte de collection. Donc, dans le code ci-dessus, remplacez simplement les boucles qui itératif "Enum.GetValues (...)" pour utiliser votre collection à la place. J'espère que cela a du sens. – ASanch