Radiobutton甚至水平对齐

时间:2012-03-18 16:13:10

标签: wpf xaml wpf-controls

有没有办法均匀布局包括radiobutton文本在内的无线电按钮?我尝试了StackPanel的Orientation = Horizo​​ntal,DockPanel和UniformGrid,但是我没有达到我想要的精确外观,这是控件之间的一个空白空间,而不必包裹或截断文本。

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>

1 个答案:

答案 0 :(得分: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>

如果此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 =“true”

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