获取文本以在单选按钮模板中对齐

时间:2017-06-07 02:14:15

标签: wpf templates radio-button textblock

我正在尝试制作一个单选按钮模板,如下所示: enter image description here

这是我添加的模板:

<Style
        x:Key="MainWindowTemplateConfigurationWindowRadioButtonStyle"
        TargetType="RadioButton"
        BasedOn="{StaticResource MainWindowRadioButtonImageStyle}">
        <Setter
            Property="Margin"
            Value="3,3" />
        <Setter
            Property="Template">
            <Setter.Value>
                <ControlTemplate
                    TargetType="{x:Type RadioButton}">
                    <Border BorderThickness="2" BorderBrush="{StaticResource BRInactiveItemBackgroundBrush}" >
                        <StackPanel x:Name="Panel" Background="{StaticResource ControlsLightBorderBrush}" VerticalAlignment="Stretch"
                                    >
                            <TextBlock x:Name="ButtonText" TextAlignment="Center" FontSize="25" Padding="7,7" VerticalAlignment="Center"
                                       />
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.SystemConfiguration}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource SystemConfigurationRadioButton}" />
                            <Setter
                                Property="Background"
                                TargetName="Panel"
                                Value="{StaticResource BRInactiveItemBackgroundBrush}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.SystemConfiguration}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource SystemConfigurationRadioButton}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.PlateSetup}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource PlateSetup}" />
                            <Setter
                                Property="Background"
                                TargetName="Panel"
                                Value="{StaticResource BRInactiveItemBackgroundBrush}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.PlateSetup}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource PlateSetup}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.ThermalCyclingProtocol}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource ThermalProtocol}" />
                            <Setter
                                Property="Background"
                                TargetName="Panel"
                                Value="{StaticResource BRInactiveItemBackgroundBrush}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.ThermalCyclingProtocol}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource ThermalProtocol}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.Reports}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="True" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource ReportsRadioButton}" />
                            <Setter
                                Property="Background"
                                TargetName="Panel"
                                Value="{StaticResource BRInactiveItemBackgroundBrush}" />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition 
                                    Binding="{Binding Tag, RelativeSource={RelativeSource Self}}"
                                    Value="{x:Static views:DisplayModes.Reports}"/>
                                <Condition
                                    Binding="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                    Value="False" />
                            </MultiDataTrigger.Conditions>
                            <Setter
                                Property="Text"
                                TargetName="ButtonText"
                                Value="{StaticResource ReportsRadioButton}" />
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

然而,我得到的结果是这样的: enter image description here

我尝试使用StackPanel和TextBlock之间的所有属性来对齐单选按钮中心的文本,但是当我确实得到它时,我得到了文本中心对齐的结果,但是内容不适合填充单选按钮的空间。我无法再附上任何图片,因为我没有足够的声誉,道歉......

我基本上可以通过将StrePach中的StackPanel的VerticalAlignment更改为Center来获得,但是它并没有正确地填充整个区域。我强迫每个RadioButton的高度,我使用这种风格与最高的按钮(称为热缩写协议)的高度相同,并且我似乎无法弄清楚如何同时制作文本集中在一起,同时填补额外的高度...

任何帮助将不胜感激。 如果它有帮助,我将如何布置单选按钮:

<Grid Margin="5,5">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0" >
                    <Grid.RowDefinitions>
                        <RowDefinition SharedSizeGroup="A"/>
                        <RowDefinition SharedSizeGroup="A"/>
                        <RowDefinition SharedSizeGroup="A"/>
                        <RowDefinition SharedSizeGroup="A"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <RadioButton Grid.Row="0" Style="{StaticResource MainWindowTemplateConfigurationWindowRadioButtonStyle}" 
                                 HorizontalAlignment="Stretch"
                                 Height="{Binding ActualHeight, ElementName=ThermalCyclingProtocolButton}"
                                 Tag="{x:Static views:DisplayModes.SystemConfiguration}">
                    </RadioButton>
                    <RadioButton Grid.Row="1" Style="{StaticResource MainWindowTemplateConfigurationWindowRadioButtonStyle}" 
                                 HorizontalAlignment="Stretch"
                                 Height="{Binding ActualHeight, ElementName=ThermalCyclingProtocolButton}"
                                 Tag="{x:Static views:DisplayModes.PlateSetup}">
                    </RadioButton>
                    <RadioButton Grid.Row="2" Style="{StaticResource MainWindowTemplateConfigurationWindowRadioButtonStyle}" 
                                 HorizontalAlignment="Stretch" x:Name="ThermalCyclingProtocolButton"
                                 Tag="{x:Static views:DisplayModes.ThermalCyclingProtocol}">
                    </RadioButton>
                    <RadioButton Grid.Row="3" Style="{StaticResource MainWindowTemplateConfigurationWindowRadioButtonStyle}" 
                                 HorizontalAlignment="Stretch"
                                 Height="{Binding ActualHeight, ElementName=ThermalCyclingProtocolButton}"
                                 Tag="{x:Static views:DisplayModes.Reports}">
                    </RadioButton>
                </Grid>
            </Grid>
        </Grid>
    </Grid>

1 个答案:

答案 0 :(得分:1)

将模板中的面板从StackPanel更改为Grid。这已经达到了我所期待的效果。