将样式应用于我的 WPF 应用程序中的所有文本框

pro*_*ton 0 wpf xaml

我想将样式(字体大小)应用于所有文本框时遇到问题,无论它们在哪里。

我当然找到了这些链接: Apply an application-level style to all textboxes

如何定位所有控件(WPF 样式)

但这对我不起作用。

第一个仍然更好,因为您不需要使用密钥,也不需要转到所有文本框。

我已经在我的 app.xml 中将样式应用于所有控件(应用颜色),所以我尝试了这样的事情(即使这对我来说还不够,因为这些样式并非无处不在):

<Style x:Key="Type1Data" TargetType="{x:Type Control}">
    <Setter Property="Background" Value="#FEE9E6"/>
    <Style.Resources>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="FontSize" Value="50" />
        </Style>
    </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

正如我所说,我也试过这个,但行不通。

<Style TargetType="{x:Type TextBox}">
                <Setter Property="FontSize" Value="50" />
            </Style>
Run Code Online (Sandbox Code Playgroud)

知道我的问题是什么以及如何实现我想要的目标吗?我能找到的所有铰链都让我回到相同的代码,但没有找到一个有效的。

编辑:这是我当前的 app.xaml

<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>
Run Code Online (Sandbox Code Playgroud)

正如我所说,当前的样式并没有覆盖整个应用程序(我添加了需要的键,或者什么都不添加)

编辑:如果我直接(作为第二个设置器)将字体大小添加到 Type 1,2 或 3,则字体大小适用。所以看起来,除了 app.xml 中的 3 之外,没有其他样式可以应用。

<Setter Property="FontSize" Value="50" />
Run Code Online (Sandbox Code Playgroud)

应该获得不同文本框大小的代码示例(一个已经有样式,一个没有),它们在网格中:

<com:ViewControl x:Class="myApp.View.ViewControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:syncfusion="http://schemas.syncfusion.com/wpf"  
             xmlns:input="http://schemas.syncfusion.com/wpf"
             xmlns:local="clr-namespace:myApp.View"
             xmlns:com="clr-namespace:RAPINF.PLL.Common;assembly=myApp.Common"
             xmlns:entities="clr-namespace:myApp.Entities;assembly=myApp.Entities"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="700">

<TextBox Style="{StaticResource Type1Data}" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" />

        <TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" Grid.ColumnSpan="3" />
Run Code Online (Sandbox Code Playgroud)

编辑:添加我有时使用的弹出窗口的代码并且有效

<sf:RibbonWindow x:Class="namespace:myApp.Common.DetailViewWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                xmlns:sf="http://schemas.syncfusion.com/wpf" 
                xmlns:self="clr-namespace:myApp.Common"
                mc:Ignorable="d" 
                d:Height="300" d:Width="400" 
                WindowStartupLocation="CenterOwner"
                >
</sf:RibbonWindow>
Run Code Online (Sandbox Code Playgroud)

我的用户控件如何添加到停靠栏:

public void AddView(UserControl View, string sTitle, DockState docState)
{
    int Width = 800;
    int Height = 400;

    DockingManager.SetHeader(View, sTitle);
    DocumentContainer.SetMDIBounds(View, new Rect(30, 30, Width, Height));
    DockingManager.SetState(View, docState);
    DockingManager.SetShowCloseMenuItem(View, true);
    DockingManager.SetDesiredWidthInDockedMode(View, Width);

    DockManager.Children.Add(View);
    ActivateView(View);
}
Run Code Online (Sandbox Code Playgroud)

如果我使用码头我之前这样做:

ApplicationContext.Current.AddView(View, DockState.Document);
Run Code Online (Sandbox Code Playgroud)

并且有一个弹出窗口,几乎相同:

    DetailViewWindow dlg = new DetailViewWindow(View);
    dlg.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

由于,这两个代码与相同的视图一起使用(是的,完全相同),那么我想问题出在在 Dock 中而不是在弹出窗口中添加视图的事实。

码头经理会强迫我使用钥匙吗?

感谢您的帮助。

Mig*_*oom 5

我无法重现你的问题。我可以向您展示一个工作示例。也许它可以帮助您找到问题。

应用程序.xaml

<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>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml

<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>
Run Code Online (Sandbox Code Playgroud)

输出

例子