2012-02-04 46 views
4

我正在關注this answer on how to databind enums (ints in my case) to RadioButtons,但是如果我有幾個TabItem,每個都有10x10個RadioButton網格,有什麼方法可以去掉一些樣板?由於是,每個單選按鈕必須有與它所有這些信息:如何減少RadioButton綁定代碼?

<RadioButton 
    IsChecked="{Binding 
     RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, 
     Path=FavoriteColor, 
     Converter={StaticResource IntToBoolConverter}, 
     Mode=TwoWay, 
     ConverterParameter=5}" 
    Content="Red" Grid.Column="4" Grid.Row="6" /> 

最好,我想能夠在TabControl的每一次設置的RelativeSource,轉換器和模式一次,路徑TabItem,並且每個RadioButton只設置ConverterParameter。這在XAML中可能嗎?如果不是,那麼在代碼隱藏方面做更有意義?

+0

相關:HTTP:/ /stackoverflow.com/questions/6258505/(我的答案使用'ItemsControl'可能會有所幫助,您可以使用'UniformGrid'作爲面板)。 – 2012-02-04 23:41:52

+0

@ H.B。哦,這意味着我不僅能夠擺脫綁定樣板,但我也能擺脫所有的RadioButton定義? – 2012-02-05 00:00:05

+0

是的,它確實... – 2012-02-05 00:13:47

回答

8

這裏將是一個進步的my answer on a related question,利用的ListBoxes單一選擇模式:

<ListBox SelectionMode="Single" SelectedItem="{Binding EnumValue}" 
     Style="{StaticResource BorderlessStyle}"> 
    <ListBox.Resources> 
     <ObjectDataProvider x:Key="items" MethodName="GetValues" 
          ObjectType="{x:Type sys:Enum}"> 
      <ObjectDataProvider.MethodParameters> 
       <x:Type TypeName="local:MainWindow+TestEnum" /> 
      </ObjectDataProvider.MethodParameters> 
     </ObjectDataProvider> 
    </ListBox.Resources> 
    <ListBox.ItemsSource> 
     <Binding Source="{StaticResource items}" /> 
    </ListBox.ItemsSource> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <!-- Automatic grid layout, adjust as needed --> 
      <UniformGrid Columns="2" /> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <RadioButton Content="{Binding}" 
        IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}" /> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

上的樣式做出的ListBox自行消失:

<Style x:Key="BorderlessStyle" TargetType="ListBox"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBox"> 
       <ItemsPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListBoxItem"> 
          <ContentPresenter /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

我發現這也很有幫助:http://msdn.microsoft.com/en-us/library/bb613576.aspx。 @ H.B。你是一個了不起的隊友。 – 2012-06-09 00:56:43

+0

我更喜歡:http://stackoverflow.com/questions/397556/how-to-bind-radiobuttons-to-an-enum但我不能說,重複使用ListBox它實際上是非常狡猾的;) – quetzalcoatl 2012-08-08 13:28:20

+0

@quetzalcoatl:You鏈接到一個問題,你指的是接受的答案?如果是這樣的話:這是一個非常糟糕的解決方案,因爲1.由於所有硬編碼值和冗餘([DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself)),維護是一場噩夢,特別是對於大枚舉2.代碼通常很長。 – 2012-08-08 13:42:04