2012-03-18 162 views
1

有沒有辦法均勻佈局單選按鈕,包括單選按鈕文本?我已經嘗試了與方向=水平,DockPanel和UniformGrid的StackPanel,但我沒有達到我要去的確切外觀,這是控件之間的空白量,而不必包裝或截斷文本。Radiobutton甚至水平對齊

StackPanel Alignment Horizontal

UniformGrid One Row

<GroupBox Name="grpLegend" Header="{x:Static res:Strings.ChartOptionsDisplayControlView_GroupBox_Legend}"> 
       <ItemsControl 
        ItemsSource="{Binding IsAsync=True, Path=AvailablePitchbookLegendPosition}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <RadioButton 
           Content="{Binding IsAsync=True, Path=DisplayName}" 
           IsChecked="{Binding IsAsync=True, Path=IsSelected}" 
           GroupName="LegendPosition" 
           Margin="2,3.5" /> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Horizontal"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 
      </GroupBox> 

回答

3
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 

    <RadioButton Grid.Column="0" Content="Left"/> 
    <RadioButton Grid.Column="1" HorizontalAlignment="Center" Content="Center"/> 
    <RadioButton Grid.Column="2" Content="Right"/> 
</Grid> 

如果電網是一個列表的ItemTemplate的一部分,你想網格的列的寬度同步,你應該使用SharedSizeGroup屬性。

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto" SharedSizeGroup="c1"/> 
     <ColumnDefinition SharedSizeGroup="c2"/> 
     <ColumnDefinition Width="Auto" SharedSizeGroup="c3"/> 
    </Grid.ColumnDefinitions> 

    <RadioButton Grid.Column="0" Content="Left"/> 
    <RadioButton Grid.Column="1" HorizontalAlignment="Center" Content="Center"/> 
    <RadioButton Grid.Column="2" Content="Right"/> 
</Grid> 

,然後一個合適的父容器上使用你的答案附加屬性Grid.IsSharedSizeScope =「真」

<ListBox Grid.IsSharedSizeScope="True" ItemTemplate={StaticResource RadioButtonTemplate}/> 
+0

感謝。這絕對適用於靜態項目。我想知道這將如何與動態收集一起工作,因此可能會有不同數量的單選按鈕。 – dior001 2012-03-18 16:24:32

+0

已更新的答案。查看Grid.IsSharedSizeScope以獲取更多示例。 – Phil 2012-03-18 16:28:57

+0

謝謝你。我知道你的答案有效,但我還沒有能夠成功地將它應用到我的ItemsControl。我很困惑你如何將收集數據綁定到包含3個單選按鈕的模板,而不是一個。如果我這樣做,我只需要9個單選按鈕而不是3個。這只是我缺乏XAML知識。 – dior001 2012-03-18 17:46:11