I'm new to this so please bear with me.
I now have this Style. Paste this into a window to see it in action.
<Window.Resources>
<Style x:Key="SelectionButton3"
TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ClipToBounds="False">
<Grid.RowDefinitions>
<RowDefinition Height="75" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="55" />
</Grid.ColumnDefinitions>
<Border x:Name="TheBorder"
BorderThickness="0,1.5,1.5,1.5"
CornerRadius="3"
Background="SteelBlue"
Height="35"
Grid.Column="1"
Grid.Row="0"
Margin="-31"
BorderBrush="DarkSlateBlue"/>
<Rectangle Name="TheRectangle"
Fill="SteelBlue"
Stroke="DarkSlateBlue"
Grid.Row="0"
Grid.Column="0">
<Rectangle.LayoutTransform>
<RotateTransform Angle="-45" />
</Rectangle.LayoutTransform>
<Rectangle.BitmapEffect>
<DropShadowBitmapEffect ShadowDepth="5" />
</Rectangle.BitmapEffect>
</Rectangle>
<ContentPresenter x:Name="ContentArea"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter TargetName="TheBorder"
Property="BitmapEffect">
<Setter.Value>
<DropShadowBitmapEffect ShadowDepth="0" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground"
Value="White"/>
<Setter Property="FontFamily"
Value="Segoe UI" />
<Setter Property="FontSize"
Value="20" />
</Style>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<Button Grid.Row="1"
Grid.Column="1"
Style="{StaticResource SelectionButton3}"
Margin="0,0,0,5"
Click="Button_Click">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Height="35"
Width="35"
Stretch="Fill"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="0"
Margin="32.5,0,0,0"
Source="/app;component/Media/Images/Mail/email.png" />
<TextBlock Text="Email"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="22,0,0,0"/>
</Grid>
</Button>
I have 2 questions:
1) Why is only part of the text showing up?
2) How do I put the Image and TextBlock in the Template and just bind to them?
The reason why your Text is clipped is because you did not specify Grid.Row and Grid.Column for your ContentPresenter. This causes your button's content to default to the 0th Row and 0th Column on your ControlTemplate, which in combination with your margins and positions causes the Textblock on your Button Element to be cut off. I am guessing this might be your intent?
<ContentPresenter x:Name="ContentArea"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Content="{Binding Path=Content,
RelativeSource={RelativeSourceTemplatedParent}}" />
Now the ContentPresenter will span both columns on the ControlTemplate's Grid, so your Textblock will have more room to display itself.
To answer your second question How do I put the Image and Textblock in the template and just bind to them?
One way to do it is to subclass the Button class, call it MyImageButton for example, and implement two new Dependency Properties called Image and Text in this new class. Then you can move your <Image> and <TextBlock> into your Control Template and bind them to your new Image and Text Properties. You can then instantiate MyImageButton and used the new Properties to set the button's image and text.
Related
I am new to the WPF technology. Currently I am willing to apply master page concept in an WPF application.
I have used ResourceDictionary for the same. I am facing an issue while doing this as per below.
I want list of link buttons to be displayed on my Master page.
As there is no direct link button control available in the WPF I have found a style script used to create a link button as per below.
<Style x:Key="Link" TargetType="Button">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline"
Text="{TemplateBinding Content}"
Background="{TemplateBinding Background}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now I have a button control used in the ResourceDictionary file and I am trying apply this style to the button control. It compiles successfully but runs with an exception message.
Below is my code of ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MSRTC.Master">
<Style TargetType="{x:Type local:Master}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Master}">
<Grid Background="LightBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="850"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="D:\Assignments\WPF\MSRTC\Developement\MSRTC\MSRTC\MSRTC\Images\Logo.png" Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image Source="D:\Assignments\WPF\MSRTC\Developement\MSRTC\MSRTC\MSRTC\Images\HeaderImage1.jpg" Grid.Column="1" Grid.Row="0" Stretch="Fill"/>
<Grid Grid.Row="1" Grid.Column="0" Background="Silver" Height="400">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Welcome" FontWeight="Bold" FontSize="15" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Button Name="btnHome" Content="Home" Grid.Row="1" Style="{StaticResource Link}"></Button>
</Grid>
<StackPanel Margin="10" Grid.Column="1" Grid.Row="1">
<!--<StackPanel Margin="10" Grid.Column="0" Grid.Row="1">-->
<ContentPresenter Content="{TemplateBinding Title}"/>
<ContentPresenter Content="{TemplateBinding Abstract}"/>
<ContentControl Content="{TemplateBinding Content}"/>
</StackPanel>
</Grid>
<!--<StackPanel Margin="10">
<ContentPresenter Content="{TemplateBinding Title}"/>
<ContentPresenter Content="{TemplateBinding Abstract}"/>
<ContentControl Content="{TemplateBinding Content}"/>
</StackPanel>-->
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Can anyone tell me where I am going wrong and how to takle with this.
Also one thing to be mention, the link button style script I have written in App.xaml Application.Resource section.
Thanks in advance.
I know too little about design to pull this off.
I am trying to make a UserControl with a special border. The border should look like this:
The header is a label (or textblock if is a must) with the content set at init time.
The border must stop before the header and start again after the header with a margin like described.
The border will house a frame or grid which must confrom to the border shape (probably with a mask) The entire background must be transparent or alpha-ed (Color #000000XX) which is important because the header cant just "hide" the rectangle by being on top.
I'd appreciate directions to achieve this. Blend from visual studio 2012 is available.
Thank you
here you go
I made use of HeaderedContentControl which allows you to have a header and a content which you can further use in a template of your preference
<HeaderedContentControl x:Class="CSharpWPF.MyUserControl"
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"
Header="Header">
<HeaderedContentControl.Template>
<ControlTemplate TargetType="HeaderedContentControl">
<Grid>
<Border BorderBrush="Black"
BorderThickness="4"
CornerRadius="10"
Padding="4"
Margin="10">
<ContentPresenter />
</Border>
<TextBlock Text="{TemplateBinding Header}"
Background="White"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="13"
Margin="25,0,0,0"
Padding="10,0"/>
</Grid>
</ControlTemplate>
</HeaderedContentControl.Template>
<Grid>
<TextBlock Text="content" />
</Grid>
</HeaderedContentControl>
result
Update
try this template, I did try to achieve by pure xaml
<HeaderedContentControl.Template>
<ControlTemplate TargetType="HeaderedContentControl">
<DockPanel x:Name="root"
LastChildFill="True"
Margin="10">
<DockPanel.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush"
Value="Black" />
<Setter Property="Width"
Value="30" />
<Setter Property="Height"
Value="30" />
<Setter Property="CornerRadius"
Value="10" />
</Style>
</DockPanel.Resources>
<Grid DockPanel.Dock="Top"
Height="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border BorderThickness="4,4,0,0" />
<Border BorderThickness="0,4,0,0"
Grid.Column="2"
Width="auto"
CornerRadius="0" />
<Border BorderThickness="0,4,4,0"
Grid.Column="3"
HorizontalAlignment="Right" />
<TextBlock Text="{TemplateBinding Header}"
FontSize="13"
Grid.Column="1"
Margin="10,-10"
VerticalAlignment="Top" />
</Grid>
<Grid Height="20"
DockPanel.Dock="Bottom">
<Border BorderThickness="4,0,4,4"
Width="auto"
VerticalAlignment="Bottom" />
</Grid>
<Border BorderThickness="4,0,0,0"
DockPanel.Dock="Left"
Height="auto"
Width="20"
CornerRadius="0" />
<Border BorderThickness="0,0,4,0"
DockPanel.Dock="Right"
Width="20"
Height="auto"
CornerRadius="0" />
<ContentPresenter Margin="-10" />
</DockPanel>
</ControlTemplate>
</HeaderedContentControl.Template>
other approach might include some code behind if this is not suitable
result
RESOLVED EDIT:
Because it is not clear through the discussion nothing was really wrong with the structure of the grids or their placement. The designer however misrepresented where the grid was. For some reason it placed the grid in the upper left hand corner of the window rather then within the content portion of the window. Once that is accounted for everything else lined up as anticipated.
I am working on a WPF project and have run into an issue with styling my forms. I needed a custom window, one without all the windows stuff; so I created a custom window style that I could apply to all my windows. Within that style I created a grid so that I could more easily place the default elements of the default window style. Then on my dashboard window I apply the default window style and all looks well. But once I attempt to place a grid within the dashboard window things start getting wonky.
In order to compensate for this I added an Adorner Decorator to the grid in the default style and placed it at the content location of that grid. This does allow me to place my content within dashboard window, but it does not follow the grid rules made by the dashboards grid.
So all I am attempting to do is create a custom window style that can be applied to all of my windows, and be able to use a grid to slice up the area designated contented by that windows default style.
This is the meaningful portion of the windows style, it lays out a grid for the boarders(drag labels), header, footer, control bar buttons(max, min, close) and content.
<Style x:Key="DefaultWindowStyle" TargetType="{x:Type Window}" >
<Setter Property="Margin" Value="5" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Background" Value="MidnightBlue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid Name="WindowGrid">
<Grid.RowDefinitions>
<RowDefinition Name="TopBorderRow" Height="5" />
<RowDefinition Name="TitleBarRow" Height="30" />
<RowDefinition Name="RContentRow" Height="*" />
<RowDefinition Name="BottomBorderRow" Height="5" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Name="LeftBorderCol" Width="5" />
<ColumnDefinition Name="CContentCol" Width="*" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="30" />
<ColumnDefinition Name="RightBorderCol" Width="5" />
</Grid.ColumnDefinitions>
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" Cursor="SizeNS" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="6" Cursor="SizeNS" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Width="5" Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Cursor="SizeWE" HorizontalAlignment="Left" VerticalAlignment="Stretch" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Width="5" Grid.Row="0" Grid.Column="5" Grid.RowSpan="4" Cursor="SizeWE" HorizontalAlignment="Right" VerticalAlignment="Stretch" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Width="5" Grid.Row="0" Grid.Column="0" Cursor="SizeNWSE" HorizontalAlignment="Left" VerticalAlignment="Top" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Width="5" Grid.Row="0" Grid.Column="5" Cursor="SizeNESW" HorizontalAlignment="Right" VerticalAlignment="Top" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Width="5" Grid.Row="3" Grid.Column="0" Cursor="SizeNESW" HorizontalAlignment="Left" VerticalAlignment="Bottom" />
<s:ResizeThumb Style="{StaticResource ThumbAsBorderStyle}" Height="5" Width="5" Grid.Row="3" Grid.Column="5" Cursor="SizeNWSE" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
<s:DragLabel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6" Style="{DynamicResource DefaultWindowTitle}" Content="{TemplateBinding Title}"/>
<s:TitleBarButton x:Name="Minimize" Style="{StaticResource TitleBarButton}" Grid.Row="1" Grid.Column="2">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{DynamicResource ButtonMinimize}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{DynamicResource ButtonMinimizeHover}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</s:TitleBarButton>
<s:TitleBarButton x:Name="Maximize" Style="{StaticResource TitleBarButton}" Grid.Row="1" Grid.Column="3">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{DynamicResource ButtonMaximize}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{DynamicResource ButtonMaximizeHover}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</s:TitleBarButton>
<s:TitleBarButton x:Name="Close" Style="{StaticResource TitleBarButton}" Grid.Row="1" Grid.Column="4">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{DynamicResource ButtonClose}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="{DynamicResource ButtonCloseHover}"/>
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</s:TitleBarButton>
<AdornerDecorator Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="4">
<ContentPresenter />
</AdornerDecorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And here is the code of the dashboard window where I am attempting to place fitted elements in their correct grid slots (those created by the dashboard window). The grid created blow looks as though all of the rows and columns are in the right spot, and their elements should simply fit, but they have strange margins. So the StudySessionPanel(which is 128x234) should fit perfectly within row 1, col 2, but as you can see it require colsaps, margins and other junk in order to place it at the correct location.
Title="Dashboard"
Style="{DynamicResource DefaultWindowStyle}" mc:Ignorable="d"
Height="840" Width="1024">
<Grid Name="DashboardGrid" Width="1024" Height="840">
<Grid.RowDefinitions>
<RowDefinition Height="63" />
<RowDefinition Height="128" />
<RowDefinition Height="234" />
<RowDefinition Height="18"/>
<RowDefinition Height="380" />
<RowDefinition Height="16" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16" />
<ColumnDefinition Width="15.25" />
<ColumnDefinition Width="234" />
<ColumnDefinition Width="15.25" />
<ColumnDefinition Width="280" />
<ColumnDefinition Width="15.25" />
<ColumnDefinition Width="417" />
<ColumnDefinition Width="15.25" />
<ColumnDefinition Width="16" />
</Grid.ColumnDefinitions>
<cc:PanelStudySession x:Name="StudySessionPanel" Grid.ColumnSpan="3" Margin="15,66,16,62" Grid.RowSpan="2" Grid.Row="1">
</cc:PanelStudySession>
<cc:PanelPerformance x:Name="PerformancePanel" Grid.Column="3" Grid.ColumnSpan="4" Margin="0,66,10,62" Grid.Row="1" Grid.RowSpan="2">
</cc:PanelPerformance>
<cc:PanelProgress x:Name="ProgressPanel" Grid.Column="1" Grid.ColumnSpan="4" Margin="0,190,10,62" Grid.Row="2" Grid.RowSpan="3">
</cc:PanelProgress>
<cc:PanelHistory x:Name="HistoryPanel" Grid.Column="5" Grid.ColumnSpan="2" Margin="0,190,15,62" Grid.Row="2" Grid.RowSpan="3">
</cc:PanelHistory>
</Grid>
This is the image with the margins, and row/col spans
Ignoring the designer the elements sort of line up, though some of them get chopped. It looks the same when ran as it does in the presenter.
Your StudySessionPanel is actually being put in rows 1-2 (Row=1, RowSpan=2) and columns 0-2 (no column and ColumnSpan=3). Just set Grid.Row and Grid.Column if you want to put it in a cell.
I'm currently trying to implement a Metro styled Window.
So i've made the following styles inside a ResourceDictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Brushes -->
<SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFFFF" />
<!-- Buttons -->
<Style x:Key="MetroControlBoxButton" TargetType="Button">
<Setter Property="Background" Value="{StaticResource BackgroundColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Windows -->
<Style x:Key="MetroWindow" TargetType="Window">
<Setter Property="UseLayoutRounding" Value="True" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="ResizeMode" Value="NoResize" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<Grid Background="{StaticResource BackgroundColor}">
<Grid.RowDefinitions>
<RowDefinition Height="6" />
<RowDefinition Height="24" />
<RowDefinition Height="*" />
<RowDefinition Height="24" />
<RowDefinition Height="6" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="6" />
</Grid.ColumnDefinitions>
<Rectangle Name="topLeftBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="0" />
<Rectangle Name="topCenterBorderRectangle" Fill="Orange" Grid.Row="0" Grid.Column="1" />
<Rectangle Name="topRightBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="2" />
<Rectangle Name="middleLeftBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" />
<Rectangle Name="middleRightBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="2" />
<Rectangle Name="bottomLeftBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="0" />
<Rectangle Name="bottomCenterBorderRectangle" Fill="Orange" Grid.Row="4" Grid.Column="1" />
<Rectangle Name="bottomRightBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="2" />
<Rectangle Name="statusBarRectangle" Fill="Yellow" Grid.Row="3" Grid.Column="1" />
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
<ColumnDefinition Width="28" />
</Grid.ColumnDefinitions>
<Rectangle Name="dragRectangle" Fill="Yellow" Grid.Row="0" Grid.Column="1" />
<Button Name="minimizeButton" Content="_" Grid.Row="0" Grid.Column="2" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.MinimizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Button Name="maximizeButton" Content="[]" Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.MaximizeNormalizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.CloseCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</Grid>
<ContentPresenter Grid.Row="2" Grid.Column="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
My problem is that i have no idea how to implement de drag feature.
My dragRectangle doesn't have a Command property so how can i call DragMove() on MouseLeftButtonDown on a Rectangle using MVVM?
Thanks
A ResourceDictionary can have code behind just like Windows etc. so you could add an event handler and call DragMove from there
Setting up the code behind requires a couple of steps.
If your ResourceDictionary is called MetroStyleResourceDictionary.xaml you add a new file in Visual Studio in the same folder called MetroStyleResourceDictionary.xaml.cs
The code behind file should then look like this
public partial class MetroStyleResourceDictionary
{
//...
}
After that you need to add the x:Class attribute to the Xaml file
<ResourceDictionary x:Class="YourNamespace.MetroStyleResourceDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--...-->
</ResourceDictionary>
Now you can add an event handler to the dragRectangle for MouseLeftButtonDown. You'll also need to get a hold of the Window so binding that to Tag might be a good idea
<Rectangle Name="dragRectangle"
MouseLeftButtonDown="dragRectangle_MouseLeftButtonDown"
Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
.../>
And finally you can add the event handler to the code behind file which will look like this
public partial class MetroStyleResourceDictionary
{
void dragRectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Rectangle dragRectangle = sender as Rectangle;
Window window = dragRectangle.Tag as Window;
if (window != null)
{
window.DragMove();
}
}
}
I am having a form in maximize mode, within the form contains a HeaderContentControl.
Within the HeaderContentControl.Content, i added a DockLayout, but the problem is that DockLayout is not fit to the form height.
How can I resolve this problem? Here's the xaml file:
<Window x:Class="Prototype.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Prototype"
Title="XXX"
x:Name="frmMain"
Width="581" Height="340" ResizeMode="CanMinimize"
WindowStartupLocation="CenterScreen" WindowState="Maximized"
WindowStyle="None" IsHitTestVisible="True" Topmost="False" AllowsTransparency="True" Background="Transparent" Loaded="frmMain_Loaded">
<!-- Copyright Microsoft Corporation. All Rights Reserved. -->
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainWindow}">
<Border Background="#FF333333"
BorderBrush="#FFCCCCCC"
BorderThickness="1"
CornerRadius="5"
Padding='2'>
<HeaderedContentControl>
<HeaderedContentControl.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="19*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="212*" />
<ColumnDefinition Width="84*" />
<ColumnDefinition Width='Auto' />
</Grid.ColumnDefinitions>
<Rectangle Grid.ColumnSpan="3" Fill="#FF505050" />
<TextBlock FontSize="13"
FontWeight='Bold'
VerticalAlignment='Center'
Margin="6,5,3,6"
Text="XXX" Grid.ColumnSpan="2" OpacityMask="#FFCECECE" Foreground="#FFF3F3F3" Height="20" />
<Button x:Name='WindowCloseButton'
Grid.Column="2"
Width="17"
Height="17"
Cursor='Hand'
Margin="8,6,6,8"
VerticalAlignment='Center'
Click='WindowCloseButton_Click' FontFamily="Lucida Console">
<Button.Background>
<ImageBrush />
</Button.Background>
<Image Source="/Prototype;component/Resource/window-close.png"></Image>
</Button>
</Grid>
</HeaderedContentControl.Header>
<!-- New Content Area -->
<HeaderedContentControl.Content>
<ContentPresenter Content="{TemplateBinding Content}" />
</HeaderedContentControl.Content>
</HeaderedContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Foreground" Value="#FF7B7B7B"></Setter>
<Style.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Foreground" Value="#333333"></Setter>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#333333"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="23" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu Height="23" Name="menuContext" Margin="0,0" Background="#FF7B7B7B" Foreground="White" Grid.Row="0">
<MenuItem Header="File" Background="#FF7B7B7B" Foreground="White">
<MenuItem Header="Open" Margin="0,1"/>
<MenuItem Header="Save" Margin="0,1"/>
<MenuItem Header="Exit" Margin="0,1" UseLayoutRounding="True" />
</MenuItem>
</Menu>
<Grid Grid.Row="1" ShowGridLines="True">
<DockPanel LastChildFill="True">
<Border Height="25"
Background="SkyBlue"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25"
Background="SkyBlue"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Top">
<TextBlock Foreground="Black">Dock = "Top"</TextBlock>
</Border>
<Border Height="25"
Background="LemonChiffon"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Bottom">
<TextBlock Foreground="Black">Dock = "Bottom"</TextBlock>
</Border>
<Border Width="200"
Background="PaleGreen"
BorderBrush="Black"
BorderThickness="1"
DockPanel.Dock="Left">
<TextBlock Foreground="Black">Dock = "Left"</TextBlock>
</Border>
<Border Background="White"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Foreground="Black">This content will "Fill" the remaining space</TextBlock>
</Border>
</DockPanel>
</Grid>
</Grid>
</Window>
Problem here is HeaderedContentControl uses StackPanel internally to layout header and content.
To fix that, use a Grid instead or re-template HeaderedContentControl to use Grid.
Example:
<ControlTemplate TargetType="HeaderedContentControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Content="{TemplateBinding Header}" Grid.Row="0" />
<ContentControl Content="{TemplateBinding Content}" Grid.Row="1" />
</Grid>
</ControlTemplate>
Adding a similar answer to the accepted one, but which uses a DockPanel instead of a Grid for a simpler implementation with the same results.
Example with DockPanel:
<ControlTemplate TargetType="HeaderedContentControl">
<DockPanel>
<ContentControl Content="{TemplateBinding Header}" DockPanel.Dock="Top" />
<ContentControl Content="{TemplateBinding Content}" />
</DockPanel>
</ControlTemplate>