2009-09-15 49 views
6

我在XAML以下組合框元素:如何使用ObservableCollection源實現XAML單選按鈕控件?

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

我想實現單選按鈕同樣的方式,像這樣:

僞代碼:

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

但是,唯一的WPF Rad我可以找到的ioButton實現是這樣的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> 

如何創建一個單選按鈕控制,也不是一成不變的,如上面的,而是從它的ItemsSource屬性獲取其數據在上面的ComboBox的例子嗎?

回答

5

使用的ItemsControl和DataTemplate中:

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

你不需要來包裝?至少DataContext仍然引用父級的DataContext而不是項目類型。 請參閱:http://stackoverflow.com/q/1511516/134761 – angularsen 2014-09-18 11:57:21

+0

@angularsen正確! – 2017-10-13 08:22:24

1

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

此行

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

代碼是利用項目源屬性

C#代碼

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

public partial class Window1 : Window 

{ 
    public Window1() 

    { 

     InitializeComponent(); 

    } 

} 

} 

我認爲這將幫助你..

+0

您應該使用'ItemsControl'而不是'ListBox',因爲後者的選擇功能不是必需的。 – 2013-05-21 11:03:41