2009-09-15 13 views
6

tengo el siguiente ComboBox elemento en XAML:¿Cómo implementar un control de botón de radio XAML con un origen ObservableCollection?

<ComboBox ItemsSource="{Binding CollectionControlValues}" 
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Value}" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

me gustaría poner en práctica RadioButtons en el misma manera, así:

pseudo-código:

<RadioButtons ItemsSource="{Binding CollectionControlValues}" 
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}"> 
    <RadioButtons .ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Value}" /> 
     </DataTemplate> 
    </RadioButtons .ItemTemplate> 
</RadioButtons > 

Sin embargo, el único WPF Rad Las implementaciones de ioButton que puedo encontrar son static de esta manera.

<StackPanel x:Name="rbHolder1" Style="{StaticResource rbStackPanelStyle}"> 
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 1</RadioButton> 
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 2</RadioButton> 
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 3</RadioButton> 
    <RadioButton Style="{StaticResource rbStyle}">...</RadioButton> 
</StackPanel> 

¿Cómo puedo crear un control RadioButton que no es estático, como los anteriores sino que obtiene sus datos de su propiedad ItemsSource como en el ejemplo anterior ComboBox?

Respuesta

5

Uso ItemsControl y DataTemplate:

<ItemsControl ItemsSource="{Binding CollectionControlValues}"> 
    <DataTemplate> 
    <RadioButton Content="{Binding Value} IsChecked={Binding SomeProperty}" GroupName="name"/> 
    </DataTemplate> 
</ItemsControl> 
+1

¿Usted no necesita para envolver el dentro de un ? Al menos DataContext aún se refería al DataContext del padre en lugar del tipo de elemento. Ver: http://stackoverflow.com/q/1511516/134761 – angularsen

+0

@angularsen ¡correcto! –

1

código en el archivo de Window1.xaml

<Window x="RadioButton.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:local ="clr-namespace:RadioButton" 
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"> 
<StackPanel> 
    <StackPanel.Resources> 
     <ObjectDataProvider x:Key="RadioOptions" MethodName="GetValues" ObjectType="{x:Type sys:Enum}"> 
      <ObjectDataProvider.MethodParameters> 
       <x:Type TypeName="local:RadioOption" /> 
      </ObjectDataProvider.MethodParameters> 
     </ObjectDataProvider> 
     <Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}"> 

      <Setter Property="BorderBrush" Value="{x:Null}" /> 

      <Setter Property="BorderThickness" Value="0" /> 

      <Setter Property="ItemContainerStyle"> 

       <Setter.Value> 

        <Style TargetType="{x:Type ListBoxItem}" > 

         <Setter Property="Margin" Value="2" /> 

         <Setter Property="Template"> 

          <Setter.Value> 

           <ControlTemplate TargetType="{x:Type ListBoxItem}"> 

            <Border Background="Transparent"> 

             <RadioButton Focusable="False" 

        IsHitTestVisible="False" 

        IsChecked="{TemplateBinding IsSelected}"> 

              <ContentPresenter /> 

             </RadioButton> 

            </Border> 

           </ControlTemplate> 

          </Setter.Value> 

         </Setter> 

        </Style> 

       </Setter.Value> 

      </Setter> 

     </Style> 
    </StackPanel.Resources> 
    <ListBox Margin="37,20,28,58" Name="listBox1" Style="{StaticResource RadioButtonList}" ItemsSource="{Binding Source={StaticResource RadioOptions}}" /> 
</StackPanel> 

esta línea

<ListBox` Margin="37,20,28,58" Name="listBox1" Style="{StaticResource RadioButtonList}" 
ItemsSource="{Binding Source={StaticResource RadioOptions}}" 

es utilizar el elemento de fuente de la propiedad

código C#

namespace RadioButton 
{ 
    public enum RadioOption 
    { 
    option1, 
    option2, 
    option3, 
    option4 
    } 

public partial class Window1 : Window 

{ 
    public Window1() 

    { 

     InitializeComponent(); 

    } 

} 

} 

creo que esto le ayudará a cabo ..

+0

Probablemente debería utilizar 'ItemsControl' en lugar de' ListBox', ya que la funcionalidad de selección de este último no es necesaria. –

Cuestiones relacionadas