Specify width/height as resource in WPF - 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>

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...

How could providing Parent Window as Pattern - WPF

Consider I have 4 Window on my project and I try to provide specific Closing button and one Title
How could I make a object of window and all of window use it as Pattern.
Here is Example of what we have for pattern window:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" AllowsTransparency="True" >
<Grid>
<Button Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" X:Name="WindowTitle/>
</Grid>
</Window>
How could I use for all of my Window as pattern. Thanks
Actually, there is no need to write a parent window. You can use Style and Template instead. It's more convenient and is recommended by Microsoft WPF Team.
Write the code below into your App.xaml and you'll get the picture above:
<Application x:Class="Walterlv.Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
.........>
<Application.Resources>
<Style x:Key="Style.Window.Default" TargetType="Window">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
Text="{TemplateBinding Title}"/>
<Border Grid.Row="1" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<!-- This is the container to host your Window.Content -->
<ContentPresenter/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
And you can use only one property Style to share such a "pattern":
<Window x:Class="Walterlv.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{StaticResource Style.Window.Default}">
</Window>
You can define different kinds of styles in the App.xaml file and select anyone in your XxxWindow.xaml you need.

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 canvas add element from resources

I am new to wpf
<Window.Resources>
<Ellipse x:Key="connectorNode" Height="20" Width="20" Fill="Green" Stroke="Black" StrokeThickness="2" MouseMove="Ellipse_MouseMove" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown" MouseLeftButtonUp="Ellipse_MouseLeftButtonUp"></Ellipse>
</Window.Resources>
How can i add an instance of the Ellipse in the resources to a canvas, i only want to specify Canvas.Left and Canvas.Right but use the same property values as in resources
<Canvas>
</Canvas>
It seems like you want to create a generic style and apply it to every Ellipse added, this is how you can do it:
<Window x:Class="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 x:Key="EllipseStyle" TargetType="Ellipse">
<Setter Property="Height" Value="20"/>
<Setter Property="Width" Value="20" />
<EventSetter Event="Control.MouseMove" Handler="Control_MouseMove" />
</Style>
</Window.Resources>
<Grid>
<Canvas>
<Ellipse Style="{StaticResource EllipseStyle}" />
</Canvas>
</Grid>
</Window>
Edit: I've added "EventSetter" so your events can be defined in the style (see this post).
If you really want to use an element from resources and apply only canvas properties you can do it this way:
<Window.Resources>
<ControlTemplate x:Key="connectorNode" >
<Ellipse
Height="20"
Width="20"
Fill="Green"
Stroke="Black"
StrokeThickness="2"
MouseMove="Ellipse_MouseMove"
MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"
MouseLeftButtonUp="Ellipse_MouseLeftButtonUp" />
</ControlTemplate>
</Window.Resources>
<Canvas>
<ContentControl Template="{StaticResource connectorNode}"/>
<ContentControl Canvas.Left="50" Template="{StaticResource connectorNode}"/>
</Canvas>

Setting window icon with a static resource

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.

Resources