I cannot find any example how to implement a scrollviewer that contains a form, with the save/cancel buttons directly underneath and not separated at the bottom of the window here is a simple layout. I need to support resizing the window. I have many different lengths of forms, I want the scrollviewer to be as small as possible, and fill available space only if needed (if the form is larger). I don't want the buttons to always be pushed to the bottom of the window Not like this!, but to hug closer to the form (in the case of a shorter form) Something like this.
In my grid, I have a row for the ScrollViewer, a row for the buttons, and a row to fill remaining space (if any).
I understand that by setting the row height=Auto kills the ScrollViewer. How can I implement this then? Is this even possible?
Jeff -> Here is an image showing the window height increased. And an image showing the window height decreased, the buttons can disappear from view, and the scrollviewer overflows so that you cannot see all of it
<Window x:Class="WpfAppRenderPathGeometry.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:WpfAppRenderPathGeometry"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="ScrollRow"/>
<RowDefinition Height="80" x:Name="ButtonsRow"/>
<RowDefinition Height="*" x:Name="SpaceRow"/>
</Grid.RowDefinitions>
<ScrollViewer x:Name="MyScroll" VerticalScrollBarVisibility="Visible">
<Grid x:Name="MyForm">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Column="1"></TextBox>
<TextBlock Grid.Row="1" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="1" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="2" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="2" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="3" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="3" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="4" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="4" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="5" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="5" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="6" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="6" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="7" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="7" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="8" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="8" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="9" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="9" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="10" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="10" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="11" Grid.Column="0">LABEL</TextBlock>
<TextBox Grid.Row="11" Grid.Column="1"></TextBox>
</Grid>
</ScrollViewer>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="1">
<Button>CANCEL</Button>
<Button>SAVE</Button>
</StackPanel>
</Grid>
</Window>
As an example, I think this will do what you want - just adjust as you need.
I adjusted again but I am not sure I understand your intent.
<Window
x:Class="WpfSubDivideWindow.ScrollViewWindow"
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:local="clr-namespace:WpfSubDivideWindow"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:MyNamespace="clr-namespace:WpfSubDivideWindow"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="ScrollViewWindow"
Width="800"
Height="350"
mc:Ignorable="d"
>
<Window.Resources>
<Style x:Key="tbk.Base"
TargetType="TextBlock"
>
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0,12,20,12" />
</Style>
<Style x:Key="tbx.Base"
TargetType="TextBox"
>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0,12,60,12" />
</Style>
<Style x:Key="Btn.Base"
TargetType="Button"
>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Padding" Value="0" />
<Setter Property="Background" Value="DarkGreen" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="20,0,0,0" />
<Setter Property="Height" Value="36" />
<Setter Property="Width" Value="90" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="ScrollRow" MaxHeight="{Binding ElementName=MyForm, Path=ActualHeight}" />
<RowDefinition x:Name="ButtonsRow" Height="80" />
<RowDefinition x:Name="SpaceRow" Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer x:Name="MyScroll"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Visible"
>
<Grid x:Name="MyForm"
Background="Moccasin"
>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Grid.Row="0"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="0"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="1"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="1"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="2"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="2"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="3"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="3"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="4"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="4"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="5"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="5"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="6"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="6"
Style="{StaticResource tbx.Base}"
/>
<TextBlock
Grid.Column="0"
Grid.Row="7"
Text="INDEX"
Style="{StaticResource tbk.Base}"
/>
<TextBox
Grid.Column="1"
Grid.Row="7"
Style="{StaticResource tbx.Base}"
/>
</Grid>
</ScrollViewer>
<StackPanel
Grid.Row="1"
HorizontalAlignment="Center"
Orientation="Horizontal"
>
<Button
Content="Cancel"
Margin="0"
Style="{StaticResource Btn.Base}"
/>
<Button
Content="Save"
Style="{StaticResource Btn.Base}"
/>
</StackPanel>
</Grid>
</Window>
Related
can say that I am a beginner in WPF.
I have a strange behavior of GridSplitter; the grid is divided into 5 roww.
tabbar
horizontal grid
splitter
another tabbar
a textbox (txtLog)
like code below, but when I moving the separator the txtLog moves down instead of following the previous tabbar.
XAML:
<Window x:Class="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:MSCSimulator"
mc:Ignorable="d"
FontSize="14"
Title="Simulator"
Height="500" Width="800"
WindowStartupLocation="CenterScreen"
WindowStyle="ThreeDBorderWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" MinHeight="50" />
<RowDefinition Height="*" MinHeight="120" />
<RowDefinition Height="10" />
<RowDefinition Height="50" MinHeight="50" />
<RowDefinition Height="*" MinHeight="120" />
</Grid.RowDefinitions>
<!-- TOOLBAR -->
<ToolBarTray Margin="10,10,10,10" Width="Auto" Height="30">
<ToolBar Width="Auto" Height="30">
<Button Click="Send_Click">
<StackPanel Orientation="Horizontal">
<Image Source="/Simulator;component/Images/play.png" Width="12" Height="12" />
<Label Padding="5,0,0,0">Invia</Label>
</StackPanel>
</Button>
</ToolBar>
</ToolBarTray>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="220" MinWidth="150" MaxWidth="220" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TreeView Width="Auto" Margin="10,0,0,0">
<TreeViewItem Header="Commands" IsExpanded="True">
<!-- APPLICATION -->
<TreeViewItem Header="Application">
<TreeViewItem Header="Simulate" MouseDoubleClick="Application_Simulate_MouseDoubleClick" />
<TreeViewItem Header="Update" MouseDoubleClick="Application_Update_MouseDoubleClick" />
</TreeViewItem>
</TreeViewItem>
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
</TreeView>
<GridSplitter Width="10"
Background="White"
VerticalAlignment="Stretch"
ResizeBehavior="BasedOnAlignment" />
<!-- JSON -->
<TextBox Name="txtJSON"
Grid.Column="2"
Background="White"
TextWrapping="Wrap"
AcceptsReturn="True"
Margin="-10,0,10,0"
VerticalScrollBarVisibility="Visible"
Width="Auto" Height="Auto" />
</Grid>
<GridSplitter Grid.Row="2"
Height="10"
ResizeDirection="Rows"
HorizontalAlignment="Stretch"
ResizeBehavior="PreviousAndNext"
Background="White"/>
<!-- TOOLBAR -->
<ToolBarTray Grid.Row="3" Margin="10,0,10,0" Width="Auto" Height="30" VerticalAlignment="Top">
<ToolBar Width="Auto" Height="30">
<Button Click="Clear_Click">
<StackPanel Orientation="Horizontal">
<Image Source="/Simulator;component/Images/clear.png" Width="16" Height="16" />
<Label Padding="5,0,0,0">Cancella</Label>
</StackPanel>
</Button>
</ToolBar>
</ToolBarTray>
<!-- LOGGER-->
<TextBox Name="txtLog"
Grid.Row="4"
TextWrapping="Wrap"
Margin="10,-10,10,10"
AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
Width="Auto" Height="Auto"
IsReadOnly="True">
<TextBox.Background>
<SolidColorBrush Color="#282828"></SolidColorBrush>
</TextBox.Background>
<TextBox.Foreground>
<SolidColorBrush Color="White"></SolidColorBrush>
</TextBox.Foreground>
</TextBox>
</Grid>
thanks advance
In your outer Grid, try to put the Height from * to Auto in the second RowDefinition
...
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" MinHeight="50" />
<RowDefinition Height="Auto" MinHeight="120" />
<RowDefinition Height="10" />
<RowDefinition Height="50" MinHeight="50"/>
<RowDefinition Height="*" MinHeight="120" />
</Grid.RowDefinitions>
<!-- TOOLBAR -->
...
I'm trying to draw a vertical line under a specific column.so anyone tell me how to draw a line horizontally and vertically
<Window x:Class="WPFDataGrid.GroupBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GroupBox" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="186"/>
<RowDefinition Height="72"/>
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<GroupBox Header="Select Option" Height="150" HorizontalAlignment="Left" Name="gb1" VerticalAlignment="Top" Width="250" BorderBrush="Black" BorderThickness="1" Margin="11" Padding="11">
<StackPanel>
<CheckBox Content="Add Cream" Margin="3.5"/>
<CheckBox Content="Add Suger" Margin="3.5"/>
<CheckBox Content="Add Flavering" Margin="3.5"/>
<CheckBox Content="Add Biscuit" Margin="3.5"/>
</StackPanel>
</GroupBox>
<Separator Grid.Row="1" HorizontalAlignment="Center" BorderThickness="1" BorderBrush="Black" />
</Grid>
You can use a <Separator/> Link
or maybe a GridSplitter Link
here is a sample of both:
<Grid x:Name="grid1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="259"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Text="Some text"/>
<StackPanel Grid.Row="1">
<TextBox Text="Some text"/>
<Separator />
<TextBox Text="2nd text box"/>
</StackPanel>
<TextBox Text="Some Text" Grid.Column="1" />
<GridSplitter Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="3" Background="Black"/>
<GridSplitter Grid.Row="0" Grid.ColumnSpan="2" Width="Auto" Height="3" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Background="Black"/>
</Grid>
It's Pretty Easy. Pick your Grid location and Y1, Y2 for a vertical line or X1, X2 for a Horizontal Line. Below is an example for a vertical line.
<Line Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" Stroke="AliceBlue" StrokeThickness="4" Y1="100" Y2="600" Margin="20,0,0,0"></Line>
<Grid>
<Grid.Resources>
<Style x:Key="lineStyle" TargetType="Line">
<Setter Property="Stroke" Value="Gray" />
<Setter Property="Stretch" Value="Fill" />
<Setter Property="Grid.ZIndex" Value="100" />
</Style>
<Style x:Key="horizontalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}">
<Setter Property="X2" Value="1" />
<Setter Property="X1" Value="0" />
<Setter Property="VerticalAlignment" Value="Bottom" />
<Setter Property="Grid.ColumnSpan"
Value="{Binding
Path=ColumnDefinitions.Count,
RelativeSource={RelativeSource AncestorType=Grid}}" />
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
</Style>
<Style x:Key="verticalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}">
<Setter Property="Y2" Value="1" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Grid.RowSpan"
Value="{Binding
Path=RowDefinitions.Count,
RelativeSource={RelativeSource AncestorType=Grid}}" />
<Setter Property="Stroke" Value="Black"/>
<Setter Property="StrokeThickness" Value="1"/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Line Style="{StaticResource horizontalLineStyle}" />
<Line Grid.Row="0" Style="{StaticResource horizontalLineStyle}" />
<Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}" />
<Line Grid.Column="0" Style="{StaticResource verticalLineStyle}" />
<Line Grid.Column="1" Style="{StaticResource verticalLineStyle}" />
<Line Grid.Column="2" Style="{StaticResource verticalLineStyle}" />
<Line Grid.Column="3" Style="{StaticResource verticalLineStyle}" />
<Line Grid.Column="4" Style="{StaticResource verticalLineStyle}" />
<Label Content="" Grid.Row="0" Grid.Column="0" HorizontalContentAlignment="Center" FontWeight="Bold" Foreground="White"/>
<Label Content="Dword" Grid.Row="0" Grid.Column="1" HorizontalContentAlignment="Center" FontWeight="Bold" Foreground="White"/>
<Label Content="Words" Grid.Row="0" Grid.Column="2" HorizontalContentAlignment="Center" FontWeight="Bold" Foreground="White"/>
<Label Content="Bytes" Grid.Row="0" Grid.Column="3" HorizontalContentAlignment="Center" FontWeight="Bold" Foreground="White"/>
<Label Content="Bits" Grid.Row="0" Grid.Column="4" HorizontalContentAlignment="Center" FontWeight="Bold" Foreground="White"/>
</Grid>
I have customized the template of LabeledPieChart (source: Bea) in a Style. Then I created a DataTemplate for the Slices of the LabeledPieChart. Both the style and the DataTemplate works great if they are alone. If they are together the Style overrides the DataTemplate that he has no effect. Is it possible to combine these two?
Here is my Code (The Style and the DataTemplate are in Window.Resources):
Style:
<Style TargetType="customControls:LabeledPieChart" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="customControls:LabeledPieChart">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="9*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<dataVis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" Margin="0,5,0,3"/>
<Grid Grid.Row="1" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<chartPrmtvs:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
<Grid Canvas.ZIndex="-1" Background="Transparent" />
<Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="0" />
</chartPrmtvs:EdgePanel>
</Grid>
</Grid>
<dataVis:Legend x:Name="Legend" Style="{TemplateBinding LegendStyle}" Title="{TemplateBinding LegendTitle}"
BorderThickness="0" Background="Transparent" FontSize="15" Grid.Column="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
DataTemplate:
<DataTemplate DataType="{x:Type local:City}" >
<Border BorderThickness="1" BorderBrush="Gray">
<StackPanel Background="White" Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type customControls:PieChartLabel}}, Path=FormattedRatio}" VerticalAlignment="Center" Margin="5,0,5,0" />
<TextBlock Text="- " />
<TextBlock Text="{Binding Path=Population}" Margin="5,0,5,0" />
<TextBlock Text="- " />
<TextBlock Text="{Binding Name}" Margin="0,0,5,0"/>
</StackPanel>
</Border>
</DataTemplate>
LabeldPieChart:
<Grid>
<customControls:LabeledPieChart
x:Name="labeledPieChart"
Title="Population of Puget Sound Cities"
Height="500" Width="700"
Grid.Row="3"
BorderBrush="Gray"
>
<customControls:LabeledPieChart.Series>
<customControls:LabeledPieSeries
x:Name="labeledPieSeries"
ItemsSource="{Binding}"
IndependentValuePath="Name"
DependentValuePath="Population"
IsSelectionEnabled="True"
LabelDisplayMode="Auto"
/>
</customControls:LabeledPieChart.Series>
</customControls:LabeledPieChart>
</Grid>
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 found this code which replaces the default select style with a custom style for the select ListBoxItem in a ListBox. However, there is still a little blue line on the left from the default style which I can't remove with any padding or margin changes.
How can I remove that blue line and completely determine the style of the selected ListBoxItem?
alt text http://tanguay.info/web/external/blueLineLeft.png
<Window x:Class="CodingContext.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CodingContext"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="ItemTemplate" DataType="{x:Type local:Person}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" />
<TextBlock Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="SelectedTemplate" DataType="{x:Type local:Person}">
<Grid Background="Yellow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Foreground="Black" Grid.Row="0" Grid.Column="0" Text="Person" FontSize="14" FontWeight="Bold" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Address" FontSize="14" FontWeight="Bold" />
<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<TextBlock Text="{Binding Address}" />
<TextBlock Text="{Binding City, StringFormat= \{0\}}" />
<TextBlock Text="{Binding State, StringFormat= \{0\}}" />
<TextBlock Text="{Binding Zip, StringFormat= \{0\}}" />
</StackPanel>
</Grid>
</DataTemplate>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
<Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Setter Property="Margin" Value="5" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<ListBox HorizontalContentAlignment="Stretch" Margin="10" x:Name="lstPeople" ItemsSource="{Binding People}" ItemContainerStyle="{StaticResource ContainerStyle}" />
</Window>
I'm pretty sure this is a bug, as several people have had this issue with the ListBoxItem template.
To fix, just add:
<Setter Property="Padding" Value="0,0,0,0"/>
to your ContainerStyle template. It's originally 2,0,0,0.
Have a look at this answer to a similar question. Basically, if you want to restyle the ListBox so that selected items have a different background colour, it's easier to simply change the "HighlightBrush" colour so that the selected item uses the colour you want.