Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义样式不显示在页面中_C#_Wpf_Xaml_Formatting - Fatal编程技术网

C# 自定义样式不显示在页面中

C# 自定义样式不显示在页面中,c#,wpf,xaml,formatting,C#,Wpf,Xaml,Formatting,我已经为此工作了一段时间,遇到了一个我无法解决的难题 我拥有的自定义样式适用于某些元素,而不适用于其他元素。例如,它适用于字体,甚至可以格式化按钮字体,而我只规定了文本块字体 它将忽略的大多数其他设置,例如面板、堆栈面板。 在绝望中,我设置了stackpanel和panel以及NavigationWindow和Pages。我已经尝试将设置更改为许多不同的元素来测试它 此外,一些代码完成将显示其中一些的选项,而其他的则不会。这似乎与渲染的内容和未渲染的内容无关 我怀疑我错过了一个重要环节。 感谢您

我已经为此工作了一段时间,遇到了一个我无法解决的难题

我拥有的自定义样式适用于某些元素,而不适用于其他元素。例如,它适用于字体,甚至可以格式化按钮字体,而我只规定了文本块字体

它将忽略的大多数其他设置,例如面板、堆栈面板。 在绝望中,我设置了stackpanel和panel以及NavigationWindow和Pages。我已经尝试将设置更改为许多不同的元素来测试它

此外,一些代码完成将显示其中一些的选项,而其他的则不会。这似乎与渲染的内容和未渲染的内容无关

我怀疑我错过了一个重要环节。 感谢您的帮助

应用程序XAML

<Application x:Class="My.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style x:Key="MyWindowStyle">
        <Setter Property="Panel.Background" Value="Chocolate" />
        <Setter Property="StackPanel.Background" Value="Chocolate" />
        <Setter Property="StackPanel.HorizontalAlignment" Value="Stretch" />
        <Setter Property="TextBlock.FontWeight" Value="Bold" />
        <Setter Property="TextBlock.FontSize" Value="20" />
        <Setter Property="Button.Background" Value="White" /> .. ETC

.. 等
页面XAML

<Page x:Class="My.HomePage" 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" Style="{StaticResource MyWindowStyle}"
    mc:Ignorable="d" Title="HomePage">      

主窗口XAML

<NavigationWindow x:Class="My.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="My App" Height="Auto" Source="HomePage.xaml"
    Style="{StaticResource MyWindowStyle}" WindowState="Maximized"></NavigationWindow>

我认为问题是由不同UI元素的混合样式造成的。您应该使用样式中的
TargetType=
属性为
TextBox
es、
StackPanel
s等定义单独的样式

此外,根据我的经验,为
窗口设置样式(没有
页面
的经验)通常不起作用。我恢复到在
窗口
本身上设置其属性