[英]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}"
如果我對您的理解正確,這就是我所做的!
希望能幫助到你。
您不必要地使情況復雜化了……您不需要其他大小的字體, DockPanel
或Border
。 代替所有這些,只需讓內容確定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.