Setting window icon with a static resource - wpf

I am trying to set the window icon in a WPF application that uses MahApps.Metro. I don't know how to set a static resource that I defined after the window's XAML to be the window's icon.
Below is the XAML, the resource I want is in Icons.xaml:
<controls:MetroWindow x:Class="Test1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Here is an example of how I use the resource on the window:
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_alien}" />
</Rectangle.Fill>
</Rectangle>
How do I set appbar_alien as the window's icon?

I've just answered to a question about icon too big in MahApps MetroWindow (Application icon stretches to title bar height when using MahApps.Metro) and same technique could give you a solution.
Basically, you have to set MetroWindow.IconTemplate property:
<MahApps:MetroWindow.IconTemplate>
<DataTemplate>
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="4"
Background="Transparent"
RenderOptions.EdgeMode="Aliased"
RenderOptions.BitmapScalingMode="HighQuality">
<Rectangle>
<Rectangle.Fill>
<VisualBrush Visual="{StaticResource appbar_alien}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</MahApps:MetroWindow.IconTemplate>
Depending of your "/Resources/Icons.xaml", you may have to stretch/scale it.

Related

Define Brush as Resource getting its color from another resource

I want to define a brush which gets its color from another resource. Just like that
<Color x:Key="MyColor">#003C83</Color>
<Brush x:Key="MyColor.Brush" Color="{StaticResource MyColor}" />
How do I do that?
You should use a specific type of Brush as it is an abstract class as #elgonzo stated... Here is a simple example with SolidColorBrush:
<Window x:Class="XAMLTest.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:XAMLTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Color x:Key="MyColor">#003C83</Color>
<SolidColorBrush x:Key="MyColor.Brush" Color="{StaticResource MyColor}" />
</Window.Resources>
<Grid>
<Border BorderBrush="{StaticResource MyColor.Brush}" BorderThickness="5" Background="Yellow" Height="20" Width="100" HorizontalAlignment="Center"/>
</Grid>
</Window>
Result:
Do try to read the WPF Brushes Overview on MSDN...

Different style of progress bar in Mahapps

I don't know why Mahapps progress bar still has the same style despite the fact that I change style in App.xaml (other controls works fine).
Is there any other way than creating from zero a style for this control?
App.xaml
<Application x:Class="WpfApp2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<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" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Orange.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
MainWindow.xaml
<Controls:MetroWindow x:Class="WpfApp2.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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800"
Background="WhiteSmoke">
<StackPanel VerticalAlignment="Center">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200*"/>
</Grid.RowDefinitions>
<Controls:Tile Grid.Column="0" Grid.Row="0" Width="200" Height="200"/>
<Controls:Tile Grid.Column="1" Grid.Row="0" Width="200" Height="200"/>
<Controls:Tile Grid.Column="2" Grid.Row="0" Width="200" Height="200"/>
</Grid>
<Controls:MetroProgressBar Value="50" />
</StackPanel>

WPF button focus style being overriden when placed in content presenter

The Setup
I have the following style that I apply to most of the windows in my application:
<ResourceDictionary x:Class="MyNamespace.ChromeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="CustomChromeTest" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid x:Name="GridMain" Background="{StaticResource MainFormColor}">
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
I use it to customize the window chrome around my window (I have removed that part) so all the actual contents of the window go inside the content presenter. I use this style like so:
<Window x:Class="MyNamespace.Window1"
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"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300"
Style="{DynamicResource CustomChromeTest}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WPFControlLibrary;component/Resources/ChromeWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
</StackPanel>
This XAML above produces a window that looks like this:
The Problem
In a normal window, when the user is tabbing through the controls, the current button is highlighted to show the user the current button. Note the border around the 3rd button below.
For some reason, the moment I use my style, this feature disappears and there is no indication of which button has focus, even though the user can tab like normal. Why is this and how can I restore the built-in functionality.
Please Note
I use this style with dozens of windows and hundreds of controls. I do not want to use a style on every control with a trigger that displays a border when the control has focus. I want to restore the default functionality that was being used before I applied my custom window style.
You'll just need to define the ContentPresenter as AdornerDecorator for Window and it will work as intended. Which I didn't figure out at first either apparently :)
Style;
<Style x:Key="CustomChromeTest" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid x:Name="GridMain" Background="Yellow">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and your window...
<Window
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:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Name="Testeroo"
x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d" Style="{StaticResource CustomChromeTest}"
Title="MainWindow" Height="550" Width="750">
<StackPanel>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
<Button Margin="5" Content="Button" Width="75"/>
</StackPanel>
</Window>
Hope this helps, cheers.

WPF: How do I handle events in a DataTemplate that is in a separate file?

I have a DataTemplate with a ListView control. This DataTemplate is located in Templates.xaml (which is a ResourceDictionary). Template.xaml is then included into my main UserControl SourceManager.xaml through ResourceDictionary.MergedDictionaries. I want to raise the SelectionChanged event of the DataTemplate's ListView but I want the handler in the code behind to be in SourceManager.xaml.cs.
How can I achieve that?
Templates.xaml:
<ResourceDictionary x:Class="LawBib.Templates"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="SectionTemplate">
<StackPanel>
<TextBlock Text="{Binding XPath=#Title}" />
<ListView x:Name="GroupList" ItemsSource="{Binding XPath=Source}">
<ListView.Template>
<ControlTemplate>
<WrapPanel IsItemsHost="True">
</WrapPanel>
</ControlTemplate>
</ListView.Template>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="images/source.png" />
<TextBlock Text="{Binding XPath=#Title}" HorizontalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</DataTemplate>
SourceManager.xaml:
<UserControl x:Class="LawBib.SourceManager"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#f5f7f8">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
<ResourceDictionary Source="Templates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
...
</UserControl>
Since SelectionChanged is a RoutedEvent, you can apply it to your UserControl like so:
<UserControl ...
ListView.SelectionChanged="MyEventHandler" />
Be aware that this event handler will be called for all Selector derived classes (as Selector is where the event is defined and raised) that are descendants of your UserControl, which includes ComboBox, Menu, ListBox, etc.
Create a Behavior
drop it in the Data Template.
that's it.

Specify width/height as resource in WPF

Is there a way in WPF to specify a width/height as a resource, so that it can be reused in several styles for e.g. margin/padding?
Sure.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<sys:Double x:Key="Height">200</sys:Double>
<sys:Double x:Key="Width">200</sys:Double>
</Page.Resources>
<Grid>
<Rectangle
Height="{StaticResource Height}"
Width="{StaticResource Width}"
Fill="Blue"/>
</Grid>
</Page>

Resources