WPF:如何将单选按钮显示为一行水平的切换按钮

8

我目前正在构建一个将在触控面板上使用的UI。因此,我希望将任何RadioButton组显示为ToggleButtons的水平行。我已经找到了如何显示ToggleButtons而不是标准的项目符号的方法:

    <Style x:Key="{x:Type RadioButton}" 
           TargetType="{x:Type RadioButton}" 
           BasedOn="{StaticResource {x:Type ToggleButton}}">

然而,这将显示一个ToggleButton的列,而不是一行。您知道一个简单的方法来做到这一点吗?
非常感谢!
2个回答

15

将单选按钮放在 Orientation 设置为 Horizontal 的 StackPanel 中。

<StackPanel Orientation="Horizontal">
   <RadioButton Content="1"/>
   <RadioButton Content="2"/>
   <RadioButton Content="3"/>
</StackPanel >

谢谢!我在我的样式定义中也做了类似的事情 :-) - Jan

3

已经明白了:单选按钮不参与解决方案——我必须修改托管它们的ItemsControl:

<Style x:Key="myKey" TargetType="{x:Type ItemsControl}">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"
                            IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接