将样式应用于我的WPF应用程序中的所有文本框
我有一个问题,而我想应用一个样式(字体大小)到我的所有文本框,无论它们在哪里将样式应用于我的WPF应用程序中的所有文本框,wpf,xaml,Wpf,Xaml,我有一个问题,而我想应用一个样式(字体大小)到我的所有文本框,无论它们在哪里 <Style TargetType="{x:Type TextBox}"> <Setter Property="FontSize" Value="50" /> </Style> 一、 当然,我们找到了这些链接: <Style TargetType="{x:Type TextBox}">
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
一、 当然,我们找到了这些链接:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
但这对我不起作用
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
第一个更好,因为你不需要使用键,也不需要进入你所有的文本框
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
在我的app.xml中,我已经将样式应用于所有控件(应用颜色),因此我尝试了类似的方法(即使这对我来说还不够,因为这些样式并不普遍):
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
如果我使用码头,我以前会这样做:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
ApplicationContext.Current.AddView(View, DockState.Document);
弹出窗口时,几乎相同:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
DetailViewWindow dlg = new DetailViewWindow(View);
dlg.ShowDialog();
由于这两个代码用于相同的视图(是的,完全相同),那么我猜问题来自于在dock中添加视图的事实,而不是在弹出窗口中
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
码头经理是否强制我使用钥匙?
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
谢谢你的帮助。我无法重现你的问题。我可以给你看一个有效的例子。也许这有助于你发现自己的问题
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
App.xaml
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Application x:Class="myApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="TextBox"><!-- tried adding those 3 lines too-->
<Setter Property="FontSize" Value="50"/>
</Style>
<Style x:Key="Type1Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="Type2Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="White"/>
</Style>
<Style x:Key="Type3Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Green"/>
</Style>
</Application.Resources>
</Application>
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize"
Value="50" />
</Style>
</Application.Resources>
</Application>
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow"
Name="MyWindow"
SizeToContent="WidthAndHeight">
<TextBox Width="150"/>
</Window>
我无法重现您的问题。我可以给你看一个有效的例子。也许这有助于你发现自己的问题
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
App.xaml
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Application x:Class="myApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="TextBox"><!-- tried adding those 3 lines too-->
<Setter Property="FontSize" Value="50"/>
</Style>
<Style x:Key="Type1Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="Type2Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="White"/>
</Style>
<Style x:Key="Type3Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Green"/>
</Style>
</Application.Resources>
</Application>
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize"
Value="50" />
</Style>
</Application.Resources>
</Application>
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow"
Name="MyWindow"
SizeToContent="WidthAndHeight">
<TextBox Width="150"/>
</Window>
我对样式也没有问题。但我通常会将我的东西分解成不同的字典,用于不同的控件,并从我自己的一些类派生出来。我还定义了一个样式,并为其指定了一个“x:Key”名称。这就像说我有一个具有这个键名的类,我希望它看起来像这样。然后,一旦我让它工作起来,我就上最后一节课,说根据定义的关键使用你的风格。。。话虽如此,这里是App.xaml的基础
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Application.Resources>
<Style TargetType="TextBox" x:Key="STextBox">
<Setter Property="FontSize" Value="50"/>
<Setter Property="FontFamily" Value="WingDings" />
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource STextBox}" />
</Application.Resources>
“x=”是它获取控件类型的库的别名。通过一般性地声明
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
TargetType="TextBox"
您没有专门关联到类类型
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
然后在我的主窗口(以及应用程序中的任何其他地方),我的主网格中有这个
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Testing" />
<TextBox Grid.Row="1" Text="Another Line" />
<TextBox Grid.Row="2" Text="Last" />
</Grid>
它们都能正常工作。您还可以将默认颜色、大小、字体系列、边距等更改为基本样式,所有这些都将更改,而无需显式引用每个控件。我对样式也没有问题。但我通常会将我的东西分解成不同的字典,用于不同的控件,并从我自己的一些类派生出来。我还定义了一个样式,并为其指定了一个“x:Key”名称。这就像说我有一个具有这个键名的类,我希望它看起来像这样。然后,一旦我让它工作起来,我就上最后一节课,说根据定义的关键使用你的风格。。。话虽如此,这里是App.xaml的基础
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Application.Resources>
<Style TargetType="TextBox" x:Key="STextBox">
<Setter Property="FontSize" Value="50"/>
<Setter Property="FontFamily" Value="WingDings" />
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource STextBox}" />
</Application.Resources>
“x=”是它获取控件类型的库的别名。通过一般性地声明
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
TargetType="TextBox"
您没有专门关联到类类型
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
然后在我的主窗口(以及应用程序中的任何其他地方),我的主网格中有这个
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Testing" />
<TextBox Grid.Row="1" Text="Another Line" />
<TextBox Grid.Row="2" Text="Last" />
</Grid>
它们都能正常工作。您还可以将默认颜色、大小、字体系列、页边距等更改为基本样式,所有内容都将更改,而无需对每个控件进行明确引用。您可以向我们展示您的App.xaml吗?请为您的问题提供一个最小、完整且可验证的示例:@MightyBadaboom Added:,我没有把整件事都加进去。mm8:我提供了app.xml,但除此之外,我显示了我尝试过的所有内容,结果是,我的文本框的字体大小没有显示任何内容。为什么您希望所有文本框都显示Type1Data、Type2Data和Type3Data样式?您应该在App.xaml中添加一个TargetType为TextBox的隐式样式(不带x:Key)。@mm8这只是我尝试过的一些方法。你的意思是:?我已经试过了(编辑代码)。如果没有,你能解释更多吗?我不习惯这些全球风格。你能给我们看看你的App.xaml吗?请提供一个关于你的问题的最小、完整和可验证的例子:@MightyBadaboom补充道:因为没有比我展示的更多的东西,所以我没有添加全部内容。mm8:我提供了app.xml,但除此之外,我显示了我尝试过的所有内容,结果是,我的文本框的字体大小没有显示任何内容。为什么您希望所有文本框都显示Type1Data、Type2Data和Type3Data样式?您应该在App.xaml中添加一个TargetType为TextBox的隐式样式(不带x:Key)。@mm8这只是我尝试过的一些方法。你的意思是:?我已经试过了(编辑代码)。如果没有,你能解释更多吗?我不习惯这些全局样式。是的,这是我找到的代码(请参阅我问题的链接)。这正是我的问题:什么应该起作用,不幸的是不起作用。你必须提供一个我们可以重现问题的最小样本。否则这几乎是不可能的。也许在其他地方有一种样式会覆盖App.xaml中的样式。添加了一个精度:如果我直接添加到类型1、2或3,字体大小将应用(样式中的第二个setter),它会工作,因此除了类型1、2或3之外,似乎没有其他样式可以应用。因为它不使用Fontsize。。。但由于这三种风格并不普遍,我需要一些其他的东西。我理解你的意思,但必须有一些东西干扰。否则就行了。这就是为什么这是一个很好的开始来分离这个问题。取决于项目的大小。有时,它会帮助删除所有其他内容(一步一步)以检查是什么造成了麻烦。使用版本控制系统时,您可以在发现问题后立即恢复。是的,这是我找到的代码(请参阅我问题的链接)。这正是我的问题:什么应该起作用,不幸的是不起作用。你必须提供一个我们可以重现问题的最小样本。否则这几乎是不可能的。也许在其他地方有一种样式会覆盖App.xaml中的样式。添加了一个精度
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>