2011-06-16 154 views
2

我需要把我的窗口3個單選按鈕,並讓用戶只選擇一個按鈕。 我做了一個ListBox並設置了選擇模式= Single,但我仍然可以選擇所有這些,我需要將每個項目都包含在內容中......我不知道該怎麼做。誰能幫忙?也許有另一種方式來呈現單選按鈕並只選擇一個......?選擇單選按鈕,wpf

這裏是XAML -

<ListBox SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0" Margin="0,0,0,57" HorizontalAlignment="Right" Width="304" Height="146" VerticalAlignment="Bottom"> 
     <ListBoxItem> 
      <RadioButton Content="Option 1" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left" Name="radioButton1" VerticalAlignment="Top" FontSize="12" /> 
     </ListBoxItem> 
     <ListBoxItem> 
      <RadioButton Content="Option 2" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left" Name="radioButton2" VerticalAlignment="Top" FontSize="12" /> 
     </ListBoxItem> 
     <ListBoxItem> 
      <StackPanel Orientation="Horizontal" Height="90"> 
       <RadioButton Content="Another : " Checked="radioButton4_Checked" Height="16" HorizontalAlignment="Left" Name="radioButton4" VerticalAlignment="Top" FontSize="12" /> 
       <TextBox Width="225" Name="TextBox_AnotherReason" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/> 
      </StackPanel> 
     </ListBoxItem> 
    </ListBox> 
+0

雖然以下各種與GroupName相關的答案是正確的,但它們並沒有解決更重要的問題:爲什麼在這裏使用ListBox?通過將單選按鈕放入列表框中,您已經引入了兩種機制*這兩種機制*都提供了從列表中選擇單個項目的方法。它們有效地競爭,所以你的例子不能真正正常工作也就不足爲奇了。要麼使用ListBox,要麼使用一組RadioButton。不要同時使用兩者。 – 2011-06-16 12:14:06

+0

好評,如果你使用它作爲佈局複選框的方式,你最好使用stackpanel。 – 2011-06-16 14:57:15

回答

9

嘗試在單選按鈕元素(參見http://arcanecode.com/2007/09/20/the-wpf-radiobutton/)GroupName屬性!

<StackPanel> 
    <RadioButton GroupName=「One「 IsChecked=「True「>Option 1</RadioButton> 
    <RadioButton GroupName=「One「 IsChecked=「False「>Option 2</RadioButton> 
    <RadioButton GroupName=「Two「 IsChecked=「False「>Option 3</RadioButton> 
    <RadioButton GroupName=「Two「 IsChecked=「True「>Option 4</RadioButton> 
</StackPanel> 

所以你的情況:

<ListBox SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent" BorderThickness="0" Margin="0,0,0,57" HorizontalAlignment="Right" Width="304" Height="146" VerticalAlignment="Bottom"> 
    <ListBoxItem> 
     <RadioButton GroupName=「Group1「 Content="Option 1" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left" Name="radioButton1" VerticalAlignment="Top" FontSize="12" /> 
    </ListBoxItem> 
    <ListBoxItem> 
     <RadioButton GroupName=「Group1「 Content="Option 2" Margin="0,0,0,10" Height="16" HorizontalAlignment="Left" Name="radioButton2" VerticalAlignment="Top" FontSize="12" /> 
    </ListBoxItem> 
    <ListBoxItem> 
     <StackPanel Orientation="Horizontal" Height="90"> 
      <RadioButton GroupName=「Group1「 Content="Another : " Checked="radioButton4_Checked" Height="16" HorizontalAlignment="Left" Name="radioButton4" VerticalAlignment="Top" FontSize="12" /> 
      <TextBox Width="225" Name="TextBox_AnotherReason" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"/> 
     </StackPanel> 
    </ListBoxItem> 
</ListBox> 
+0

已經評論過了,你是否需要它成爲一個列表框? – 2011-06-16 15:03:39

+0

不,我不需要它是一個ListBox,我有點困惑......在所有的評論後,我看到我的錯誤......謝謝。 – 2011-06-19 05:16:12

2

給他們(單選按鈕)全部組名是一樣的。

2

您需要賦予每個RadioButton一個GroupName屬性,並讓它們在您希望相互排斥的按鈕之間相同。