Frame Page controls layout - wpf

Could you please tell me where I have gone wrong, my frame size takes the size of the wpf window, code is:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BSP" Height="700" Width="990" WindowStartupLocation="CenterScreen">
<Grid Background="Green" VerticalAlignment="Center" HorizontalAlignment="Center" >
<Frame Name="MainFrame" Source="LoginScreen.xaml" NavigationUIVisibility="Hidden" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Grid>
</Window>
The page size less than the window and also the total size of margin+width+size of label is less than the size of page yet the button is being displayed as cut. Code for the page is below:
<Page x:Class="Loggedin"
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"
Height="660" Width="920"
Title="Loggedin">
<Grid Background="white" Margin="2">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" >
<Label Content="Loggedin as" Height="28" HorizontalAlignment="Left" Name="Loggedinas" />
<Button Content="LogOut" Margin="780,0,0,0" Height="28" Name="Logout" Width="50" HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
</Page>
Could you please assist me, it is eating my brain from 2 days :(. Following are pics of output:
Pic from Visual Studios: http://clip2net.com/s/1Fpb7
Pic from Runtime: http://clip2net.com/s/1FpbQ

Have you thought about using a DockPanel instead of a StackPanel? It seems to be a closer fit for the layout you're trying to achieve :
<DockPanel VerticalAlignment="Top">
<Label Content="Loggedin as" Height="28" HorizontalAlignment="Left" Name="Loggedinas" />
<Button Content="LogOut" Height="28" Name="Logout" Width="50" HorizontalAlignment="Right"/>
</DockPanel>
Any time you have margins that look like Margin="780,0,0,0" it usually means you're working too hard and there's a better way to do it. In my quick scratch tests, the 780 pixel left margin was pushing the button off the frame.

Related

Change the height of the title bar

Question: Can we change the height of the title bar displayed in MahApps.Metro?
Details: For instance, in the following XAML example from the MahApps team, I want to display the content Deploy CupCake - of the TextBlock - below the image of the CupCake. So I removed the Orientation="Horizontal" from the StackPanel in the following XAML. As shown in the snapshot below, the content Deploy CupCake is now showing below the image of CupCake - but it is hiding almost all of it. How can we make this content show all of it below the CupCake image?
Snapshot of the Toolbar with MahApps.Metro: Only about 10% of the content is showing below the image.
<mah:MetroWindow x:Class="SampleApp.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:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<mah:MetroWindow.LeftWindowCommands>
<mah:WindowCommands>
<Button Click="LaunchGitHubSite" ToolTip="Open up the GitHub site">
<iconPacks:PackIconModern Width="22"
Height="22"
Kind="SocialGithubOctocat" />
</Button>
</mah:WindowCommands>
</mah:MetroWindow.LeftWindowCommands>
<mah:MetroWindow.RightWindowCommands>
<mah:WindowCommands>
<Button Click="DeployCupCakes" Content="Deploy CupCakes">
<Button.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconModern Width="22"
Height="22"
VerticalAlignment="Center"
Kind="FoodCupcake" />
<TextBlock Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Button.ContentTemplate>
</Button>
</mah:WindowCommands>
</mah:MetroWindow.RightWindowCommands>
<Grid>
<!-- Your content -->
</Grid>
</mah:MetroWindow>
First of all, it is better to use a to use a panel that distributes the available space among its content, like a Grid, to prevent cutting off content as with the StackPanel. Here, the data template defines a Grid with two rows, where the TextBlock scales to its desired size and the icon takes up the remaining available space. Also note the HorizontalAlignment of the icon, it is centered.
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<iconPacks:PackIconModern Grid.Row="0"
Width="22"
Height="22"
HorizontalAlignment="Center"
Kind="FoodCupcake" />
<TextBlock Grid.Row="1"
Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
</Grid>
</DataTemplate>
The same result could also be achieved using a DockPanel, where the last child (in this case the icon) takes up the remaining available space, so the order of the defined controls is important.
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Bottom"
Margin="4 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
<iconPacks:PackIconModern DockPanel.Dock="Top"
Width="22"
Height="22"
HorizontalAlignment="Center"
Kind="FoodCupcake" />
</DockPanel>
</DataTemplate>
In both cases you will get the result below, a button with a centered icon and a text below.
To make the button more prominent, change the title bar height with the TitleBarHeight property.
<mah:MetroWindow x:Class="SampleApp.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:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
GlowBrush="{DynamicResource MahApps.Brushes.Accent}"
ResizeMode="CanResizeWithGrip"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
TitleBarHeight="50">

WPF - button with text and image

Cant make button in WPF app with image and text on it. My code is like this:
<Window x:Class="WindowR.One"
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:WindowR"
mc:Ignorable="d"
Title="One" Height="300" Width="300">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="Click Here" />
<Image Source="D:\Skola\4. semester\TP\GIT folder\Visualko\Core\WindowR\Pictures\0.png" />
</StackPanel>
</Grid>
</Window>
But the text isnt above image..tried lots of tutorials from here..but none of them work properly
StackPanel arranges TextBlock next to Image. Try to use Grid
<Button Width="120" Height="50" >
<Grid>
<Image Source="D:\Skola\4. semester\TP\GIT folder\Visualko\Core\WindowR\Pictures\0.png" />
<TextBlock Text="Click Here"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Grid>
</Button>

Displaying a large image in ScrollViewer in WPF

I want to display a large image in a Window where the Image will have scroll bars if it too big for the area of the screen it is in.
Underneath the image I want a button panel. For this I have put the Image inside a ScrollViewer in a DockPanel that contains a StackPanel to contain the Buttons in the Bottom part. The idea is to click the Browse button to set the image (from code behind handling Button Click)
The following example I put together will just keep the image size (2144 x 1424) and I cannot see the lower button panel.
<Window x:Class="WpfIssues.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:WpfIssues"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top"></TextBlock>
<StackPanel Orientation="Vertical">
<DockPanel x:Name="PhotoPanel">
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Click="Button_Click">Browse...</Button>
</StackPanel>
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<StackPanel>
<Image x:Name="PhotoImage"
Stretch="None"
Source="Resources/bear grills.png"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</StackPanel>
</ScrollViewer>
</DockPanel>
</StackPanel>
</DockPanel>
</Grid>
</Window>
I cant figure out why.
Try putting the button before the scrollviewer, like this:
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top" />
<Button Content="Browse..." DockPanel.Dock="Bottom" />
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image x:Name="PhotoImage"
Stretch="None"
Source="http://images6.fanpop.com/image/photos/33600000/Bear-Grylls-bear-grylls-33656894-3504-2336.jpg"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ScrollViewer>
</DockPanel>

Animation to change user controls

I have a simple custom window (XAML below).
When first loaded, it will add a user control to the grdContainer.Children collection.
User selection/action will cause other user controls to added/removed to the children collection. (one loaded at a time). What I am attempting to do is provide a simple animation as the new control is loaded, something like a 45degree swipe from top left to bottom right.
If anyone can point me in the right direction, I would appreciate it.
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WinClientFolder"
x:Name="WinClientFolder"
Title="MainWindow"
Width="450" Height="300" AllowsTransparency="True" WindowStyle="None" ResizeMode="CanResizeWithGrip">
<Window.Background>
<SolidColorBrush />
</Window.Background>
<Grid x:Name="LayoutRoot">
<Border BorderBrush="Black" BorderThickness="2,2,2,0" Margin="18,13,0,0" CornerRadius="10,10,0,0" Background="#FFCCC523" Height="32" VerticalAlignment="Top" HorizontalAlignment="Left" Width="179" Name="FolderTab">
<Grid Height="25" HorizontalAlignment="Left" Name="grdFolderTop" VerticalAlignment="Top" Width="175">
<TextBlock x:Name="txtClientName" Height="34" TextWrapping="NoWrap" Width="Auto" FontSize="18" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,0,0,0"><Run Text="Doe, John Family " /><LineBreak /><Run /></TextBlock>
</Grid>
</Border>
<Border BorderBrush="Black" BorderThickness="2,1,6,2" Margin="0,45,0,0" Background="#FFCCC523" CornerRadius="10,10,0,0" Grid.ColumnSpan="2" Name="FolderBody">
<Grid Height="Auto" Name="grdContainer" Width="Auto" />
</Border>
</Grid>
I asked a somewhat similar question in this post. I can highly recommend this Transitionals framework. It's simple to use and looks great.

Scale usercontrol in wpf?

i have multiple items in my usercontrol. I put everything in a grid. But now i am trying, if the resolution of my screen changes, the window scales automatically. This just doesn't work.
I already used viewbox, but not with the wanted result.
This is my usercontrol:
<UserControl x:Class="NewWPFVragenBeheer.Maak_toets.Views.ChangeCourse"
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"
xmlns:converters="clr-namespace:NewWPFVragenBeheer.Converters"
mc:Ignorable="d" d:DesignHeight="200" d:DesignWidth="700"
>
<UserControl.Resources>
<XmlDataProvider x:Key="Vakken"
Source="C:\Users\Ruben\Desktop\Stage 26-04\stage_TFW\stage_TFW\NewWPFVragenBeheer\Data\Courses.xml"
XPath="/Courses/Course"
/>
<converters:RadioBoolToIntConverter x:Key="radioBoolToIntConverter" />
</UserControl.Resources>
<Viewbox Stretch="None">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="722*" />
<ColumnDefinition Width="254*" />
</Grid.ColumnDefinitions>
<Label Content="Maximale tijd:" Height="28" FontWeight="Bold" HorizontalAlignment="Left" Margin="12,28,0,0" Name="label1" VerticalAlignment="Top" Width="177" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="215,30,0,0" Text="{Binding Path=MaxTime}" VerticalAlignment="Top" Width="145" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="215,2,0,0" Text="{Binding Path=ExamName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Name="textBox1" VerticalAlignment="Top" Width="145" />
<Label FontWeight="Bold" Content="Punten:" Height="28" HorizontalAlignment="Left" Margin="386,0,0,0" Name="label2" VerticalAlignment="Top" />
<TextBox Height="23" Text="{Binding Path=Score,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" HorizontalAlignment="Left" Margin="567,2,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<Label Content="{Binding Path= FeedbackText, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Height="28" HorizontalAlignment="Right" Margin="0,153,527,0" Name="label3" VerticalAlignment="Top" Width="200" Foreground="#FFF50D0D" />
</Grid>
</Viewbox>
</UserControl>
this usercontrol is set in a window:
<Window x:Class="NewWPFVragenBeheer.MaakToetsDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:NewWPFVragenBeheer.Views"
Title="MaakToetsDialog"
WindowStyle ="SingleBorderWindow"
WindowState ="Maximized"
WindowStartupLocation="CenterScreen"
>
<view:MaakToetsView />
</Window>
¨please someone help.
Set the Grid to a fixed Width and Height, and set ViewBox.Stretch to Uniform. That should do it.
The correct answer likely is more complicated than the solution you seek, but I'll try to keep it short.
To do what you want, in my experience your best bet is to use a Grid as your primary initial element and then situate your controls within that Grid (or other Grids inside of it) and wrap the individual controls in ViewBoxes. After that tie the UserControl SizeChanged event to a method that forces the height and width of the UserControl to maintain an appropriate ratio.
That's the best way I have found to handle it on my WPF UIs.

Resources