I'm styling the Grid controls to be the table headers using resources like so:
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="5,10" />
<Setter Property="Foreground" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
<Style TargetType="Border">
<Setter Property="BorderThickness" Value="0.0,1.0,0.0,0" />
<Setter Property="BorderBrush" Value="{StaticResource ForegroundDarkBrush}" />
<Setter Property="Background" Value="{StaticResource BackgroundLightBrush}" />
</Style>
</Grid.Resources>
The thing is, I need to apply that resources into multiple places in my app, which leads to the code being repeated.
I was wondering if this is possible to store the resources in my App.xaml and use them by the key or something like that? Like so:
<Resources Key="MyResourceSet">
<Style>
[..]
</Style>
</Resources>
<Grid Resource="MyResourceSet">
[...]
</Grid>
Place the Style in the App.Resources like you would in any other UIElement.
<Application x:Class="Question_Answer_WPF_App.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="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Green" />
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="100" />
</Style>
</Application.Resources>
</Application>
Reference wherever you want in your app.
<Window x:Class="Question_Answer_WPF_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</Window>
Another way to do this if you want several ResourceDictionary's to be used across your app; but with the same inner keys, is to reference the unique ResourceDictionary per element that will use it. This will not be using the App.xaml resources but will be pointing directly to the file location in your application. Since ResourceDictionary's have a default 'Build Action' of 'Page' it will work referencing the location this way. If your ResourceDictionary doesn't work this way the first thing is to check this by right clicking the ResourceDictionary in your solution explorer and make sure that's correct.
Example:
MyCustomResourcesA.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="FontSize"
Value="46" />
</Style>
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Green" />
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="100" />
</Style>
</ResourceDictionary>
MyCustomResourcesB.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Question_Answer_WPF_App">
<Style TargetType="TextBlock">
<Setter Property="FontSize"
Value="26" />
</Style>
<Style x:Key="MyButtonStyle"
TargetType="Button">
<Setter Property="Background"
Value="Blue" />
<Setter Property="Height"
Value="20" />
<Setter Property="Width"
Value="200" />
</Style>
</ResourceDictionary>
MainWindow.xaml
<Window x:Class="Question_Answer_WPF_App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<StackPanel HorizontalAlignment="Left">
<StackPanel>
<StackPanel.Resources>
<ResourceDictionary Source="MyCustomResourcesA.xaml" />
</StackPanel.Resources>
<TextBlock Text="I'm using MyCustomResourcesA" />
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</StackPanel>
<StackPanel>
<StackPanel.Resources>
<ResourceDictionary Source="MyCustomResourcesB.xaml" />
</StackPanel.Resources>
<TextBlock Text="I'm using MyCustomResourcesB" />
<Button Content="Testing"
Style="{StaticResource MyButtonStyle}" />
</StackPanel>
</StackPanel>
</Window>
Looks like:
Related
This question already has answers here:
ResourceDictionary in separate library
(3 answers)
Sharing WPF-Dictionary from another assembly
(1 answer)
Closed 3 years ago.
For a Revit plugin I'm building a WPF dialog, with a view, logic, and now some styling.
Very quickly I came to the conclusion that putting all my styling in the is really messy (it just means I'm scrolling through my XAML half of my time). So I wanted to split my styling from my XAML code (like you would with HTML and CSS).
So I found out about the and how I should go about setting it up. I have my window, with this resource code:
<Window.Resources>
<local:LocationView x:Key="mainViewDataSource" />
<FontFamily x:Key="Ubuntu">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Ubuntu</FontFamily>
<FontFamily x:Key="FontAwesomeSolid">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Solid</FontFamily>
<FontFamily x:Key="FontAwesomeRegular">pack://application:,,,/Kalec.Enveo;component/src/Resources/Fonts/#Font Awesome 5 Free Regular</FontFamily>
<ResourceDictionary x:Key="dictionary" Source="pack://application:,,,/Kalec.Enveo;component/src/WPF/Styles/Dictionary.xaml" />
</Window.Resources>
And my Dictionaries:
Dictionary.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="MergedDictionaries">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionaries/InputStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
and InputStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="SearchInputStyle" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="30"/>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border CornerRadius="15" Background="White" BorderBrush="Transparent" x:Name="border">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2" />
<TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="Red"/>
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="BorderBrush" TargetName="border" Value="Blue"/>
</Trigger>
<DataTrigger Binding="{Binding Path=Text}" Value="">
<Setter Property="Text" Value="Vul een adres in"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is that I can't seem to use the style on my textBox:
<StackPanel Margin="10,10,0,0" Orientation="Vertical">
<StackPanel Panel.ZIndex="10" Orientation="Horizontal"
Name="SearchParameters"
>
<TextBox Width="220" Style="{DynamicResource SearchInputStyle}"/>
<Button FontFamily="{StaticResource FontAwesomeSolid}"
Content="" Foreground="#31B192" Grid.ColumnSpan="2"
Style="{DynamicResource LocationImport}"
Margin="10, 0, 0, 0"/>
</StackPanel>
I simply get the error that it could not be resolved. I looked at some solutions, and most of them tell me to include the ResourceDictionary in my App.xaml, but I don't have that file, because my project is a class library (it's a requirement for the plugin).
Can I even use Resource Dictionaries? Or is there some other way to seperate the styling or XAML in different files?
I guess you got the correct ingredients but not the correct recipe.
Here's how its working for me:
I have a class library project which defines all my styles (ControlLibrary in the image)
Here is one Button styles ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<Style x:Key="RedButton" TargetType="Button">
<Style.Setters>
<Setter Property="Background" Value="Red"/>
</Style.Setters>
</Style>
<Style x:Key="GreenButton" TargetType="Button">
<Style.Setters>
<Setter Property="Background" Value="Green"/>
</Style.Setters>
</Style>
</ResourceDictionary>
Then Here's is the single point of contact for all individual styles. Dictionary.xaml
(Note that this is using MergedDictionary)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="./Styles/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Finally to use the Styles from ClassLibrary dll into my WpfApp project (after adding reference to it) we again use MergedDictionaries..!!
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ControlLibrary;component/Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
How I can define style in App.xaml for CustomButton?
App.xaml
<Style x:Key="CustomButtonSmall" TargetType="CustomButton">
<Setter Property="FontSize" Value="14" />
</Style>
MyPage.xaml
<local:CustomButton Text="{i18n:Translate CreateAccountButton}"
Grid.Column="0" Command="{Binding CreateAccountCommand}"
Type="Normal" Style="{StaticResource CustomButtonSmall}" />
You define the style in for example Window.xaml:
<Window>
<Window.Resources>
<Style x:Key="myStyle" TargetType="Button">
<Setter Property="Background" Value="Orange" />
<Setter Property="FontStyle" Value="Italic" />
</Style>
</Window.Resources>
Then u target ur button with this:
<Button Style="{StaticResource myStyle}">Buttontext</Button>
In App.xaml
Give CustomButton whole path like TargetType="Control:CustomButton"
and define Control at the top like
xmlns:Control="clr-namespace:xyz"
<Style x:Key="CustomButtonSmall" TargetType="Control:CustomButton">
<Setter Property="FontSize" Value="14" />
</Style>
If I have a UserControl with the following two Labels inside of it's grid like so:
<Grid x:Name="mainGrid">
<Label x:Name="labelTitle"/>
<Label x:Name="labelValue"/>
</Grid>
Can I set their styles separately from within a ResourceDictionary something like:
<Style TargetType="{x:Type MyControl}">
<Style.Resources>
<Style TargetType="MyControl.mainGrid.labelTitle">
</Style>
<Style TargetType="MyControl.mainGrid.labelValue">
</Style>
</Style.Resources>
</Style>
If possible I would like to do all of this in the ResourceDictionary and not have to touch the UserControl at all.
Try using a trigger in the style based on the name.
App.xaml
<Application x:Class="WpfApplication34.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication34"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type local:MyControl}">
<Style.Resources>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Style.Triggers>
<Trigger Property="Name" Value="labelTitle">
<Setter Property="Content" Value="This is the Title" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Trigger>
<Trigger Property="Name" Value="labelValue">
<Setter Property="Content" Value="This is the Value" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</Application.Resources>
</Application>
MyControl.xaml
<UserControl x:Class="WpfApplication34.MyControl"
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:WpfApplication34"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="mainGrid">
<Label x:Name="labelTitle" />
<Label x:Name="labelValue" />
</Grid>
</UserControl>
MainWindow.xaml
<Window x:Class="WpfApplication34.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:WpfApplication34"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MyControl />
</Grid>
</Window>
Screenshot:
So I have an application with a style put directly into the App.xaml file as such:
<Application x:Class="Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="OnStartup">
<Application.Resources>
<Style x:Key="SpecialButtonStyle" TargetType="Button">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Block.Foreground" Value="White" />
<Setter Property="TextBlock.Foreground" Value="White" />
<Setter Property="TextElement.Foreground" Value="White" />
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}">
<Border Background="{TemplateBinding BorderBrush}">
<ContentControl Foreground="White">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</ContentControl>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
</Application>
I want to include this as a style in my class Library, so that any xaml projects that reference that library can see "SpecialButtonStyle" as a selectable "Style" in the designer.
I've read several articles about ResourceDictionaries and creating portable XAML controls, but I'm still confused. I basically want a collection of styles included as part of a class library.
(I can only post 2 links until I get a higher StackOverflow reputation)
http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx
http://visualstudiomagazine.com/articles/2015/03/01/everyone-gets-xaml-with-xamarinforms.aspx
Any help would be greatly appreciated. Thanks!
What you read is correct.
What you need is to create a plain ResourceDictionary inside your shared assembly with a given name.
In your App.xaml you then can include this ResourceDictionary as a MergedDictionary and therefore your whole app will have access to all of the shared dictionaries resources.
Steps:
Create another project WPF Control Library Project (doesn't matter about User or Custom)
In the new project right click -> Add -> ResourceDictionary
Paste your style in so it looks like the following:
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="SpecialButtonStyle" TargetType="Button">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Block.Foreground" Value="White" />
<Setter Property="TextBlock.Foreground" Value="White" />
<Setter Property="TextElement.Foreground" Value="White" />
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" Padding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BorderThickness}">
<Border Background="{TemplateBinding BorderBrush}">
<ContentControl Foreground="White">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</ContentControl>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
In your main project reference this new Control Library
In App.xaml reference Dictionary1.xaml
App.xaml
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="NestedXamlObjects.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Just came across this post. If you are using Xamarin.Forms or .NET MAUI, you also can do it this way.
I manage all my styles in a shared class library project (same as step 1-4 from the accepted answer) and
add the ResourceDictionary as shown below.
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:KlipperRemoteControl"
xmlns:shared="clr-namespace:AndreasReitberger.Shared;assembly=SharedMauiXamlStylesLibrary"
x:Class="KlipperRemoteControl.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<shared:DefaultTheme />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
DefaultTheme.xaml form the library.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AndreasReitberger.Shared.DefaultTheme"
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/Controls/BoxView.xaml" />
<ResourceDictionary Source="/Themes/Controls/Editor.xaml" />
<ResourceDictionary Source="/Themes/Controls/Entry.xaml" />
<ResourceDictionary Source="/Themes/Controls/Label.xaml" />
<ResourceDictionary Source="/Themes/Controls/Grid.xaml" />
<ResourceDictionary Source="/Themes/Controls/Frame.xaml" />
</ResourceDictionary.MergedDictionaries>
<Color x:Key="Transparent">Transparent</Color>
<Color x:Key="TappedBackgroundColor">#eaeaea</Color>
<Color x:Key="Green">#33AD79</Color>
<Color x:Key="LightGreen">#38ef7d</Color>
<Color x:Key="DarkGreen">#11998e</Color>
<Color x:Key="Red">#ff4a4a</Color>
<Color x:Key="DarkRed">#93291e</Color>
<Color x:Key="Orange">#F78836</Color>
<Color x:Key="DarkOrange">#F83017</Color>
<Color x:Key="Blue">#3C8CF1</Color>
<Color x:Key="LightBlue">#6dd5ed</Color>
<Color x:Key="DarkBlue">#2193b0</Color>
<Color x:Key="HyperLink">#567cd7</Color>
<Color x:Key="White">#ffffff</Color>
<Color x:Key="Black">#000000</Color>
<Color x:Key="primary-lighter">#edcacd</Color>
<Color x:Key="Liliac">#d483fc</Color>
<Color x:Key="Purpleish-Blue">#5d4cf7</Color>
<Color x:Key="Link">#567cd7</Color>
<Color x:Key="Bright-Cyan">#3cdeff</Color>
<Color x:Key="Lemon-Lime">#bdff27</Color>
<Color x:Key="Yellow">#E9B31A</Color>
<Color x:Key="Pink">#C6275C</Color>
</ResourceDictionary>
I have some styles for a Metro/win8 app:
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
But now I am making a desktop app in wpf (.net 4.5) and cannot apply styles like this in xaml to a ListView control. How do we define our own custom styles for a Desktop ListView control in xaml?
Here is an example putting the style in a windows resource dictionary.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FF171717" Opacity="0.70"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="#FFEAF32C" />
<Setter Property="BorderThickness" Value="2, 0, 0, 0" />
<Setter Property="Padding" Value="5" />
<Setter Property="Opacity" Value="40" />
</Style>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>
and if you want the style to be placed in it's own file then you can reference that file like this (my resource file is just call Dictionary1.xaml)
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ScrollViewer HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch">
<ListView>
<ListView.Items>
<Button>a</Button>
<Button>b</Button>
<Button>c</Button>
<Button>d</Button>
<Button>e</Button>
</ListView.Items>
</ListView>
</ScrollViewer>
</Grid>
</Window>