我想将 WPF 单选按钮放在图像底部的中央。目前我有一个带有图像的堆栈面板,然后是底部的单选按钮,但我喜欢它,所以单击图像会触发单选按钮。
当我尝试将图像嵌入到单选按钮中时,它只显示在按钮的右侧。我怎样才能使用静态资源来做到这一点?
这就是我将单选按钮放在顶部的方法,但我不知道调整边距是否是一个好方法。
<ControlTemplate x:Key="RadioButtonBottom" TargetType="{x:Type RadioButton}">
<RadioButton IsChecked="{TemplateBinding IsChecked}" Margin="35 0 0 0" >
<TextBlock>
<LineBreak />
<InlineUIContainer>
<ContentPresenter Margin="-50,0,0,0"
Content="{TemplateBinding ContentPresenter.Content}"
ContentTemplate="{TemplateBinding ContentPresenter.ContentTemplate}"/>
</InlineUIContainer>
</TextBlock>
</RadioButton>
</ControlTemplate>
它应该看起来像这样:
最佳答案
哇,这比我预想的要难......默认的 RadioButton
在内部使用 BulletDecorator
,而且你对子弹的控制不多安置。
因此,创建一个新的 ControlTemplate
(您所做的)似乎是最好的选择。在这篇文章的帮助下:How to configure the WPF RadioButton's circle bullet , 这是另一个建议:
注意:您需要在项目中添加对 PresentationFramework.Aero
的引用。
<Window ...
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
>
<Window.Resources>
<Style TargetType="RadioButton" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<DockPanel Background="Transparent" >
<Microsoft_Windows_Themes:BulletChrome DockPanel.Dock="Bottom" HorizontalAlignment="Center"
IsRound="true" Height="{TemplateBinding FontSize}" Width="{TemplateBinding FontSize}"
BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"
IsChecked="{TemplateBinding IsChecked}"
RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" />
<ContentPresenter RecognizesAccessKey="True"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False" >
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal" >
<RadioButton>
<Image Source="..." />
</RadioButton>
<RadioButton>
<Image Source="..." />
</RadioButton>
<RadioButton>
<Image Source="..." />
</RadioButton>
</StackPanel>
</Window>
关于c# - 将 RadioButton XAML 定位在内容的中心底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369938/