Search code examples
c#csswpfxamlxmlschema

Convert static XAML inline styles to external CSS style sheet


To start I am not familiar with XAML however I know XML/CSS exceptionally well. I've begun helping a friend add some style to their XAML application though very quickly noticed that all of their styling is inline which is a nightmare. Here is a good chunk of their code:

add-type -AssemblyName System.Windows.Controls.Ribbon, PresentationFramework

[xml]$xaml = @"
<Window Height="425" Title="TSD-Essentials" Width="1050" x:Name="Window" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:ribbon="clr-namespace:System.Windows.Controls.Ribbon;assembly=System.Windows.Controls.Ribbon" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:z="http://www.w3.org/1999/xhtml">

 <Grid x:Name="Grid">
  <Grid.RowDefinitions>
   <RowDefinition Height="AUTO"/>
   <RowDefinition Height="AUTO"/>
   <RowDefinition Height="AUTO"/>
   <RowDefinition Height="AUTO"/>
   <RowDefinition Height="AUTO"/>
   <RowDefinition Height="AUTO"/>
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
   <ColumnDefinition Width="75"/>
   <ColumnDefinition Width="155"/>
   <ColumnDefinition Width="AUTO"/>
   <ColumnDefinition Width="AUTO"/>
   <ColumnDefinition Width="AUTO"/>
   <ColumnDefinition Width="AUTO"/>
  </Grid.ColumnDefinitions>

  <Grid.Background>
   <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
    <GradientStop Color="White" Offset="0"/>
    <GradientStop Color="#52618f" Offset="1"/> 
   </LinearGradientBrush>
  </Grid.Background>

  <Label Content="Host Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Width="80" />
  <TextBox x:Name = "HostName" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Width="150"></TextBox><!--Enter host name-->

  <Label Content="Service Now:" Grid.Column="2" Grid.Row="0" />
  <TextBox Grid.Column="3" Grid.Row="0" Width="175" x:Name = "SNtextBox">Search ServiceNow</TextBox>

    </Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$window.ShowDialog()

Now I've attempted to research and add my own style sheet declarations...

External:

<StyleSheet Source="styles.css" />

Internal:

<StyleSheet>
<![CDATA[
TextBox {background-color: #f0f;}
]]>
</StyleSheet>

However I keep getting error messages. The XAML documentation I come across has various elements such as application however my friend is using window. So I don't know if there is versioning involved or not hence why I pasted a large snippet of the code I've begun to clean up.

Exception calling "Load" with "1" argument(s): "Cannot create unknown type '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}StyleSheet'."

I've been reading through documents like the following though all I end up getting are error messages:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/css/#xaml

I'm vaguely aware that this is somehow .NET related (using Windows PowerShell ISE to edit this) though I'm not familiar with this. I simply need to help him get out of inline styling or he'll never get any work done. How can I get a style sheet working and with a simple example of how to change the background-color (or whatever XAML equivalent) to define the background-color of all of the TextBox elements?


Solution

  • I'm not entirely sure if there is an efficient manner of doing this however this does work. I added the Window.Resources element just after the Window element. Here is the code for that:

    <Window [...]><!-- Shown for it's relative position in the code. -->
    
    <Window.Resources>
    <Style TargetType="TextBox">
     <Setter Property="Background" Value="#000" />
     <Setter Property="Foreground" Value="#ccc" />
     <Setter Property="HorizontalAlignment" Value="Left" />
     <Setter Property="FontSize" Value="17" />
    </Style>
    </Window.Resources>
    

    CSS :hover and :focus style appears to be a subset (trying to use minimal code):

    <Window.Resources>
     <Style TargetType="TextBox">
      <!-- CSS background-color --> 
      <Setter Property="Background" Value="#000" />
    
      <Style.Triggers>
       <!-- CSS :hover -->
       <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="BorderBrush" Value="#f0f" />
        <Setter Property="Background" Value="#222" />
        <Setter Property="Foreground" Value="#fff" />
       </Trigger>
    
       <!-- CSS :focus -->
       <Trigger Property="IsKeyboardFocused" Value="True">
        <Setter Property="BorderBrush" Value="#f0f" />
        <Setter Property="Background" Value="#222" />
        <Setter Property="Foreground" Value="#fff" />
       </Trigger>
      </Style.Triggers>
     </Style>
    </Window.Resources>