繁体   English   中英

如何在WPF用户控件的两个不同控件中更改字体大小?

[英]How to change the font size in two different controls of WPF usercontrol?

我已经创建并实现了类似于Window8 PasswordBox的WPf UserContol。 当我更改字体时,文本框和内部按钮均已更改。 不幸的是,当文本框字体大小合适时,按钮字体大小不合适。 (请参见图像-第二个按钮具有完美的按钮字体大小,但文本框没有。并且第三个按钮具有不完美的文本,但是文本框具有完美的字体大小)

在实施时如何设置两个控件的字体大小? 就像属性Button_fontSize和textbox_fontSize。

我的Usercontol XAML代码:

        <Grid.Resources>
            <Style x:Key="ButtonWithoutHover" TargetType="Button">
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Margin" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Name="border" 
                            BorderThickness="3"                                                        
                            BorderBrush="White"                            
                            Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <Border BorderBrush="White" BorderThickness="2" >
                <DockPanel Canvas.Right="2" Canvas.Top="2">
                <Button Style="{StaticResource ButtonWithoutHover}" BorderThickness="3" BorderBrush="White" DockPanel.Dock="Right" Click="onButtonClick" >
                        <Button.Content>
                            <Label Content="->" Foreground="White" />
                        </Button.Content>
                    </Button>
                    <TextBox BorderThickness="0" Name="txtNumber" DockPanel.Dock="Left" >
                    </TextBox>
                </DockPanel>
            </Border>

实施XMAL代码:

 <NumText:UserControl1 Click="UserControl1_Click" FontSize="9" Background="Red" Foreground="Yellow" Margin="160,46,206,229" />
        <NumText:UserControl1 Click="UserControl1_Click" FontSize="20" Background="Red" Foreground="Yellow" Margin="121,104,173,145" />
        <NumText:UserControl1 Background="Red" FontSize="36" Foreground="Yellow" Margin="121,180,173,69" />

Ë

我在工作中所做的一件事是创建Double资源以设置FontSize属性。 我相信它将解决您的问题:

首先,在您的XAML文件中声明名称空间:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

然后使用x:Key创建两个不同的Double “变量”:

<sys:Double x:Key="SmallFont">10</sys:Double>
<sys:Double x:Key="LargeFont">35</sys:Double>

最后设置控件的FontSize属性:

FontSize="{DynamicResource SmallFont}"

要么

FontSize="{DynamicResource LargeFont}"

如果我对您的理解正确,这就是我所做的!
希望能帮助到你。

您不必要地使情况复杂化了……您不需要其他大小的字体, DockPanelBorder 代替所有这些,只需让内容确定UserControl大小,而不是相反。 尝试使用以下简化示例( Usercontrol使用):

<Grid Margin="5">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" Text="jagadees" BorderThickness="0" 
VerticalAlignment="Center" />
    <Button Grid.Column="1" Style="{StaticResource ButtonWithoutHover}" 
Background="Red" Foreground="White" Content="->" VerticalAlignment="Center" />
</Grid>

更新>>>

我的意思是,如果您重组了XAML,那么您将看到不需要使用不同的字体大小。 但是,如果您决心这样做,那么只需实现一个简单的Converter类即可更改其中一个元素的大小:

public class DoubleToLargerDoubleConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || value.GetType() != typeof(double)) return false;
        double amountToEnlargeBy = 0;
        if (parameter == null || double.TryParse(parameter.ToString(), out amountToEnlargeBy)) return false;
        double doubleValue = (double)value;
        return doubleValue + amountToEnlargeBy;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return DependencyProperty.UnsetValue;
    }
}

正如您从未实际说过要放大哪个文本一样,我只是在猜测Button文本应该更大。 在这种情况下,您可以这样使用它:

<Label Content="->" Foreground="White" FontSize="{Binding FontSize, RelativeSource={
    RelativeSource AncestorType={x:Type Button}}, Converter={StaticResource 
    DoubleToLargerDoubleConverter}, ConverterParameter=5.0}" />

我将假设您知道如何使用Converter ,如果不知道,请告诉我。 只需在Binding.ConverterParameter属性中设置希望放大字体大小的Binding.ConverterParameter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM