2009-09-15 18 views
6

Mam następujący ComboBox element XAML:Jak zaimplementować kontrolę przycisku radia XAML za pomocą źródła ObservableCollection?

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

chciałbym wdrożyć radiobuttons w sam sposób, tak:

pseudo-kod:

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

Jednak jedyny WPF Rad Implementacje ioButton, które mogę znaleźć, są takie: static.

<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> 

Jak mogę utworzyć formant RadioButton który nie jest statyczna jak wyżej, ale zamiast pobiera dane z jego właściwość ItemsSource, jak w powyższym przykładzie ComboBox?

Odpowiedz

5

Zastosowanie ItemsControl i DataTemplate:

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

Nie trzeba owinąć wewnątrz ? Przynajmniej DataContext wciąż odwoływał się do DataContext nadrzędnego zamiast typu elementu. Zobacz: http://stackoverflow.com/q/1511516/134761 – angularsen

+0

@ prostokątna poprawna! –

1

kod w pliku 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> 

tej linii

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

jest za pomocą elementu źródło nieruchomość

kod C#

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

public partial class Window1 : Window 

{ 
    public Window1() 

    { 

     InitializeComponent(); 

    } 

} 

} 

myślę, że to będzie ci pomóc ..

+0

Powinieneś raczej używać 'ItemsControl' zamiast' ListBox', ponieważ nie jest wymagana funkcja wyboru tego ostatniego. –

Powiązane problemy