c# - 将 RadioButton XAML 定位在内容的中心底部

标签 c# wpf xaml

我想将 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/

相关文章:

C#:wpf 将组合框项添加到多个组合框

wpf - 如何从 WPF ListView 中删除多个选定项?

c# - 使用 MVVM Xamarin Forms 使用数据填充选择器

c# - 构建支持我自己的自定义事件的 EventTriggerBehavior

c# - mysql批量插入一个文本文件

c# - 自动映射器配置

c# - Endianness - 如何将 C# 整数转换为大端字节(没有字节数组,只有单个字节)?

c# - 是否可以将具有不同泛型/类型的对象列表作为参数

c# - TextBox TextChanged 事件问题

c# - 由于布局影响,打开 WPF 窗口速度很慢