App.xaml样式不适用于Windows

时间:2018-03-19 10:03:34

标签: c# wpf xaml

我希望应用程序中的所有文本框在禁用或只读时都具有深灰色背景。我已经为我的App.xaml添加了一个样式,我认为它应该在我的应用程序中应用该样式。

的App.xaml:

<Application x:Class="XXXX.Deployment.Utils.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:XXXX.Deployment.Utils" >
<Application.Resources>
    <local:InvertBoolConverter x:Key="invertBoolConverter" />
    <Style TargetType="TextBox" >
        <Style.Triggers>
            <Trigger Property="IsReadOnly" Value="True">
                <Setter Property="Background" Value="DarkGray" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="DarkGray" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Application.Resources>

样式正在设计窗口中应用,但在我实际运行应用程序时却没有。我创建了一个只有一个文本框的窗口,并将IsEnabled属性设置为False,因此我不确定为什么不应用此样式。

TestWindow.xaml:

<Window x:Class="XXXX.Deployment.Utils.TestWindow"
         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:local="clr-namespace:XXXX.Deployment.Utils"
         mc:Ignorable="d" 
         Height="200" Width="800">
<StackPanel>
    <TextBox Margin="2" IsEnabled="False" />
</StackPanel>

TestWindow.xaml.cs:

namespace XXXX.Deployment.Utils
{
    public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();
        }
    }
}

我从控制台应用程序中启动窗口并使用

TestWindow t = new TestWindow();
t.ShowDialog();

修改

我认为这个问题可能是因为App.xaml构建操作不是'ApplicationDefinition'。我们在一个公共实用程序类库中拥有所有UI,因此我们得到错误“库项目文件无法指定ApplicationDefinition元素”。即使我们的UI在类库中,我们是否有可能拥有控件的全局样式?

0 个答案:

没有答案
相关问题