MahApps Resource Dictionary error - c#

So, today I installed MahApps in my WPF project. All went smoothly until I wanted to use the build-in styles.
This is my 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"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<BitmapImage x:Key="logoImage" UriSource="Images/Logo.png" />
<BitmapImage x:Key="profileButtonImage" UriSource="Images/ProfileButton.png" />
<BitmapImage x:Key="settingsButtonImage" UriSource="Images/SettingsButton.png" />
<BitmapImage x:Key="profileIconImage" UriSource="Images/profileIcon.png" />
<BitmapImage x:Key="listenIconImage" UriSource="Images/listenIcon.png" />
<BitmapImage x:Key="statsIconImage" UriSource="Images/statsIcon.png" />
<BitmapImage x:Key="accountButtonImage" UriSource="Images/accountButton.png" />
<BitmapImage x:Key="pauseButtomImage" UriSource="Images/pause-button.png" />
<BitmapImage x:Key="playButtomImage" UriSource="Images/play-button.png" />
<BitmapImage x:Key="stopButtomImage" UriSource="Images/stop-button.png" />
<BitmapImage x:Key="doneButtonImage" UriSource="Images/doneButton.png" />
<BitmapImage x:Key="arrowButtonImage" UriSource="Images/arrow.png" />
<BitmapImage x:Key="doneButtonHoverImage" UriSource="Images/doneButtonHover.png" />
<FontFamily x:Key="Novo">/Fonts/#Novecentosanswide-Medium</FontFamily>
<Style x:Key="ButtonStyle">
<Setter Property="Border.Background" Value="#262a33" />
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="#1d2027" />
</Trigger>
</Style.Triggers>
</Style>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The BitmapImages are there for some of my buttons. My issue is that when I try to compile I get the following error:
All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
Each dictionary entry must have an associated key.
I installed all 3 of the MahApps NuGets, do you have any idea why these errors appear?

Wrap all the resources inside another ResourceDictionary and in that one add MahApp resources as MergedDictionaries

Thank's Ruben, work for me only wrap all resources inside ResourceDictionary
<Application.Resources>
<ResourceDictionary>
<!--Estilo Validação-->
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel>
<AdornedElementPlaceholder x:Name="placeholder"/>
<TextBlock FontSize="12" Foreground="Red"
Text="{Binding ElementName=placeholder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<!--FIM Estilo Validação-->
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Related

How to override ComboBox ToggleButton from Material Design for XAML?

I'm new in WPF.
I have an issue with overriding MaterialDesignComboBoxToggleButton
style. I wanna to replace "Template" setter with own, but my content from control template always ignores. Why this occurred? With other styles i haven't this problem.
Bellow code demonstrates what i need.
Overriedes.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
App.xaml
<Application x:Class="Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-Wpf" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#FFD8E1FF" SecondaryColor="#FFD8E1FF" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="/Overrides.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Add the same resource key to your custom style like this:
<Style
x:Key="MaterialDesignComboBoxToggleButton"
BasedOn="{StaticResource MaterialDesignComboBoxToggleButton}"
TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<TextBlock FontSize="50" FontWeight="Bold">$$</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and you don't need to add resources like this in Overrides.xaml file:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ComboBox.xaml" />
</ResourceDictionary.MergedDictionaries>
because the MDIX resources already included by MaterialDesignTheme.Defaults.xaml that you added in App.xaml file.
In my case override style in window not working ,but when I tried in App.xml then it works. I changed the style for toggle button by another style . the code is :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/IG/IG.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Themes/IG/IG.MSControls.Toolkit.Implicit.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Border CornerRadius="8" BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="#FF0F0F4B" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Background" Value="#FF73ADDE" />
<Setter Property="Foreground" Value="#FF150404" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Application.Resources>

WPF: Global resource accessible by key

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:

WPF dynamic resource not found in MergedDictionary but outside it is fine

I have a main Class (with an MainView.xaml) that dynamically loads various other Pages long after the MainView has been shown.
So MainView.xaml has following style defined:
<Window.Resources>
<Style x:Key="ErrorTemplate" TargetType="TextBox">
<Setter Property="FontSize" Value="30"/>
</Style>
</Window.Resources>
In one of the loaded pages I had following declaration:
<Page.Resources>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</Page.Resources>
This worked fine.
Now I needed to load other resources from an external XAML file so I used MergedDictionaries.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Guaranteed to not have an ErrorTemplate key -->
<ResourceDictionary Source="MoreStyles.xaml"/>
<ResourceDictionary>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
And now I started to receive the message that ErrorTemplate could not be resolved.
Moving it outside of the MergedDictionary fixed the problem again.
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MoreStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ErrorStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource ErrorTemplate"/>
</Style>
</ResourceDictionary>
</Page.Resources>
What is happening here?

Portable XAML Styles in a Class Library

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>

Creating styles for a ListView in a WPF Desktop application

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>

Categories

Resources