I want all textboxes in my application to have a dark grey background when they are disabled or read only. I have added a style to my App.xaml which I believe should apply the style throughout my app.
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>
The style is applying in the design window but not when I actually run the application. I created a window with just a single textbox and set the IsEnabled property to False so I am not sure why this style would not be applied.
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();
}
}
}
I launch the window from within a console application and use
TestWindow t = new TestWindow();
t.ShowDialog();
EDIT
I think this issue may be because the App.xaml build action is not 'ApplicationDefinition'. We have all our UI in a common utility class library so we get the error 'Library project file cannot specify ApplicationDefinition element'. Is it possible for us to have a global style for controls even if our UI is in a class library?