I need to add a multiple buttons inside a groupbox in wpf, but I can't. I need to create my key pad for an atm machine, with all numbers, but if I try to create a button number 2, the button number 1 disappears.
what is wrong?
<Window x:Name="Win_Users" x:Class="ATM_Simulator.WindowUsers"
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:ATM_Simulator"
mc:Ignorable="d"
Title="WindowUsers" Height="300" Width="500">
<Grid>
<GroupBox x:Name="grbx_key_pad" Header="Key Pad" HorizontalAlignment="Left" Height="220" Margin="26,10,0,0" VerticalAlignment="Top" Width="189">
<Button x:Name="btn_1" Content="1" HorizontalAlignment="Left" Margin="10,13,0,0" VerticalAlignment="Top" Width="29" Height="32"/>
</GroupBox>
<GroupBox x:Name="grbx_select_transaction" Header="Select Transaction :" HorizontalAlignment="Left" Height="88" Margin="291,23,0,0" VerticalAlignment="Top" Width="179"/>
<GroupBox x:Name="grbx_select_account" Header="Select Account" HorizontalAlignment="Left" Height="92" Margin="291,138,0,0" VerticalAlignment="Top" Width="179"/>
<Button x:Name="btn_submit" Content="Submit" HorizontalAlignment="Left" Margin="291,239,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btn_close" Content="Close" HorizontalAlignment="Left" Margin="393,239,0,0" VerticalAlignment="Top" Width="77"/>
</Grid>
You have to put a container inside the GroupBox, for example a Grid would be a good choice:
<GroupBox x:Name="grbx_key_pad" Header="Key Pad" HorizontalAlignment="Left" Height="220" Margin="26,10,0,0" VerticalAlignment="Top" Width="189">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="btn_1" Grid.Column="0" Content="1" Width="29" Height="32"/>
<Button x:Name="btn_2" Grid.Column="1" Content="2" Width="29" Height="32"/>
<!-- more buttons -->
<Button x:Name="btn_4" Grid.Row="1" Grid.Column="0" Content="4" Width="29" Height="32"/>
<!-- yet more buttons -->
</Grid>
</GroupBox>
Related
Beginner with WPF here. I am trying to make a window that has a panel in the bottom of the window, that contains two buttons side by side in the bottom right corner. basically, like this picture. but, all I have managed to code is this. The bottom panel is a mess. How do I make it look like my original design? I have tried dragging within the designer, but it is not working.
My XAML below (for the entire window - because I'm a noob. feel free to correct any mistakes).
<Window x:Class="ace.views.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"
xmlns:local="clr-namespace:ace.views"
mc:Ignorable="d"
Title="Welcome to My software " Height="450" Width="800">
<Grid VerticalAlignment="top" HorizontalAlignment="Stretch">
<StackPanel Margin="30">
<TextBlock FontFamily="Segoe UI" FontSize="30" Foreground="#0078D7">Welcome to my software</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="20" TextWrapping="Wrap" Margin="0 20">This application is here to help you to teach vocabulary to your students, and to keep track of their progress.</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="20" TextWrapping="Wrap" Margin="0 20">Let's get started in the next step.</TextBlock>
</StackPanel>
<Grid Background="#EFEFEF" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Margin="0,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="cmdSubmit"
HorizontalAlignment="Center"
VerticalAlignment="Stretch" Grid.Row="0" Width="120" Content="Next" />
<Button x:Name="cmdReset"
HorizontalAlignment="Center"
VerticalAlignment="Stretch" Grid.Row="0" Width="120" Content="Cancel" Grid.Column="1"/>
</Grid>
</Grid>
</Window>
Two RowDefinitions in outer Grid should do the trick for Window layout.
Changing ColumnDefintions for inner Grid will help to pin buttons to the right side.
<Window x:Class="ace.views.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"
xmlns:local="clr-namespace:ace.views"
mc:Ignorable="d"
Background="#EFEFEF"
Title="Welcome to My software " Height="450" Width="800">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="30">
<TextBlock FontFamily="Segoe UI" FontSize="30" Foreground="#0078D7">Welcome to my software</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="20" TextWrapping="Wrap" Margin="0 20">This application is here to help you to teach vocabulary to your students, and to keep track of their progress.</TextBlock>
<TextBlock FontFamily="Segoe UI" FontSize="20" TextWrapping="Wrap" Margin="0 20">Let's get started in the next step.</TextBlock>
</StackPanel>
<Grid Grid.Row="1" Background="#EFEFEF">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button x:Name="cmdSubmit" HorizontalAlignment="Center" Grid.Column="1" Width="120" Margin="5" Content="Next"/>
<Button x:Name="cmdReset" HorizontalAlignment="Center" Grid.Column="2" Width="120" Margin="5" Content="Cancel"/>
</Grid>
</Grid>
</Window>
Change your first Grid from Top to Stretch.
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
Put a Grid.RowDefinitions there...
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
Your second Grid, point it to Grid.Row=1
<Grid Background="#EFEFEF" Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Margin="0,0,0,0">
To find out more about RowDefinitions, Here
Currently trying to build simpliest cross-resolution app (in other words an adaptive UI). The Apps itself is intended to start at min res:1024x768 and max res: 1920x1080.
However I have no success in doing of that.
this is 1024x768 window, where everything looks good.
this is 1920x1080 window, where controls "swam". However pic adapted itself to resolution.
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024" MinHeight="768" MinWidth="1024" MaxHeight="1080" MaxWidth="1920">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="512*"/>
<RowDefinition Height="160*"/>
</Grid.RowDefinitions>
<GroupBox x:Name="groupBox_Copy" Header="Truck2" Height="148" VerticalAlignment="Top" Margin="430,503.6,468.6,0" Grid.Row="1" Grid.RowSpan="2">
<Button x:Name="button_Copy" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.245,-0.667"/>
</GroupBox>
<GroupBox x:Name="groupBox_Copy1" Header="Truck3" Height="148" VerticalAlignment="Top" Margin="752,503.6,146.6,0" Grid.Row="1" Grid.RowSpan="2">
<Button x:Name="button_Copy1" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.245,-0.667"/>
</GroupBox>
<GroupBox x:Name="groupBox_Copy2" Header="Truck1" Height="148" VerticalAlignment="Top" Margin="148,503.6,750.6,0" Grid.Row="1" Grid.RowSpan="2">
<Button x:Name="button" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.245,-0.667"/>
</GroupBox>
<Image x:Name="image" Grid.Row="1" Source="Resources/truck.png"/>
</Grid>
Layout via fixed Margins in not adaptive. There is nothing wrong in using nested Panels:
UniformGrid can arrange equal space for each child element (GroupBox).
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="512*"/>
<RowDefinition Height="160*"/>
</Grid.RowDefinitions>
<UniformGrid Rows="1" Grid.Row="2">
<GroupBox x:Name="groupBox_Copy" Header="Truck2" Height="148" VerticalAlignment="Top">
<Button x:Name="button_Copy" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75"/>
</GroupBox>
<GroupBox x:Name="groupBox_Copy1" Header="Truck3" Height="148" VerticalAlignment="Top">
<Button x:Name="button_Copy1" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75"/>
</GroupBox>
<GroupBox x:Name="groupBox_Copy2" Header="Truck1" Height="148" VerticalAlignment="Top">
<Button x:Name="button" Content="Start" HorizontalAlignment="Left" Margin="20,13,0,0" VerticalAlignment="Top" Width="75"/>
</GroupBox>
</UniformGrid>
<Image x:Name="image" Grid.Row="1" Source="Resources/truck.png"/>
</Grid>
1024x768.
1920x1080.
Looks a little better. However, the controls "swam" to left direction. As can be seen.
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="768" Width="1024" MinHeight="768" MinWidth="1024" MaxHeight="1080" MaxWidth="1920">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="80*"/>
<RowDefinition Height="512*"/>
<RowDefinition Height="160*"/>
</Grid.RowDefinitions>
<Image x:Name="image" Grid.Row="1" Source="Resources/truck.png"/>
<Grid HorizontalAlignment="Left" Height="138" Margin="10,9.6,0,0" Grid.Row="2" VerticalAlignment="Top" Width="998">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125*"/>
<ColumnDefinition Width="143*"/>
<ColumnDefinition Width="147*"/>
<ColumnDefinition Width="143*"/>
<ColumnDefinition Width="171*"/>
<ColumnDefinition Width="143*"/>
<ColumnDefinition Width="120*"/>
</Grid.ColumnDefinitions>
<GroupBox x:Name="GroupBox1" Grid.Column="1" Header="Truck1" Background="SeaShell" Grid.ColumnSpan="2" Margin="0.4,0,147.6,0">
<Button x:Name="button_Copy2" Content="Start" HorizontalAlignment="Left" Margin="59,23,-2,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-1.211,-0.299"/>
</GroupBox>
<GroupBox x:Name="GroupBox2" Grid.Column="3" Header="Truck2" Background="SeaShell" Grid.ColumnSpan="2" Margin="0.4,0,171.6,0">
<Button x:Name="button_Copy" Content="Start" HorizontalAlignment="Left" Margin="59,23,-2,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-1.211,-0.299"/>
</GroupBox>
<GroupBox x:Name="GroupBox3" Grid.Column="5" Header="Truck3" Background="SeaShell" Grid.ColumnSpan="2" Margin="0.4,0,120.4,0">
<Button x:Name="button_Copy1" Content="Start" HorizontalAlignment="Left" Margin="59,23,-2,0" VerticalAlignment="Top" Width="75"/>
</GroupBox>
</Grid>
</Grid>
I am trying to create a responsive design for my wpf application but having issue.Because when i am resizing it.it is not working.So can anyone help me out.
trying to adjust the screen based on different sizes to see if any components are reacting to that. Unfortunately they don't I am not sure what I am missing
<Window x:Class="LSLABAPP.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:LSLABAPP"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="525" MinHeight="300" MinWidth="525" ResizeMode="CanResizeWithGrip">
<Grid>
<Grid.Background>
<ImageBrush/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition MinHeight="80" Height="50"></RowDefinition>
<RowDefinition MinHeight="40" Height="40"></RowDefinition>
<RowDefinition MinHeight="40" Height="40*"></RowDefinition>
<RowDefinition MinHeight="40" Height="40*"></RowDefinition>
<RowDefinition MinHeight="40" Height="40*"></RowDefinition>
<RowDefinition MinHeight="40" Height="40*"></RowDefinition>
<RowDefinition MinHeight="40" Height="40*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100" Width="100"></ColumnDefinition>
<ColumnDefinition Width="417"/>
<ColumnDefinition Width="0"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="UserName" Grid.Column="1" HorizontalAlignment="Left" Height="25" Margin="30,10,0,0" Grid.Row="2" TextWrapping="Wrap" Text="UserName" VerticalAlignment="Top" Width="172"/>
<PasswordBox x:Name="Password" Grid.Column="1" HorizontalAlignment="Left" Margin="30,10,0,0" Grid.Row="3" VerticalAlignment="Top" Width="172" Height="25"/>
<Label x:Name="UsernameLable" Content="UserName" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2"
Grid.Column="0" VerticalAlignment="Top" Width="81" Height="26"/>
<Label x:Name="PasswordLable" Content="Password" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="3"
Grid.Column="0" VerticalAlignment="Top" Width="81" RenderTransformOrigin="0.333,2.038" Height="26"/>
<Canvas Grid.Column="1" HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="417" Background="#FF4E79EE">
<Label x:Name="label" Content="Testing" Canvas.Left="70" Canvas.Top="10" Width="260" Foreground="White" FontWeight="Bold"/>
<Label x:Name="label1" Content="DEV Environment" Canvas.Left="130" Canvas.Top="36" Foreground="red" FontWeight="Bold"/>
</Canvas>
<Canvas HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="100" Background="#FF4E79EE" />
<Image Margin="0,0,414,0" Grid.ColumnSpan="2">
<Image.Source>
<BitmapImage UriSource="Content/.ng" />
</Image.Source>
</Image>
</Grid>
</Window>
when you set a fixed height or width, then wpf won't resize.
But maybe this is a duplicate question to How to make all controls resize accordingly proportionally when window is maximized?
I just started using WPF (instead of winforms) and I'm trying to create a fixed-sized window (see image).
Problem is, whenever I run the app the bottom right corner gets messed up, having near zero space between the button and the edge. (see other image)
Here's the XAML code (mostly generated by the Visual Studio designer)
<Window x:Class="UseCaseHelper.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:UseCaseHelper"
mc:Ignorable="d"
Title="UseCaseHelper" Height="500" Width="900">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="809,441,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
I've tried googling for a solution without much success. Hopefully someone can point out what I'm doing wrong here.
I always find DockPanel more flexible in these settings. Instead of the VerticalAlighnment and the Margin you set, you can set DockPanel.Dock to Left, Right, Bottom or Top.
<DockPanel LastChildFill="False">
<Button DockPanel.Dock="Top"
Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0"
Width="75"/>
<Button DockPanel.Dock="Bottom"
Content="Button" HorizontalAlignment="Right" Margin="0,0,10,10" Width="75"/>
</DockPanel>
Note that you can also use Margin="10" for both Buttons.
However, if you want to use Grid, you can use the following structure:
<Grid>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"
Width="75"/>
<Button Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10" Width="75"/>
</Grid>
Note that in this case they will overlap if the Window is small enough.
Another option is to add RowDefinitions and ColumnDefinitions to the Grid:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button
Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"
Width="75"/>
<Button Grid.Column="2" Grid.Row="2"
Content="Button" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10" Width="75"/>
</Grid>
Its perfomance is better than the other two, if Window is very small.
I have a simple WPF application with Ribbon and few controls. Unfortunately when pressing TAB key to change focus, Ribbon somehow manages looping only within itself, other controls don't get a chance...
This is the XAML:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="136" />
<RowDefinition Height="175*" />
</Grid.RowDefinitions>
<my:Ribbon HorizontalAlignment="Stretch" Name="ribbon1" VerticalAlignment="Top">
<my:RibbonTab Header="Ribbon">
<my:RibbonGroup>
<my:RibbonComboBox Label="ComboBox" Name="ribbonComboBox1">
<my:RibbonGallery MaxColumnCount="1">
<my:RibbonGalleryCategory>
<my:RibbonGalleryItem Content="An item" />
</my:RibbonGalleryCategory>
</my:RibbonGallery>
</my:RibbonComboBox>
</my:RibbonGroup>
</my:RibbonTab>
</my:Ribbon>
<TextBox TabIndex="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="12,19,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox TabIndex="2" Height="23" HorizontalAlignment="Left" Margin="12,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Grid.Row="1" />
OK. Let me answer myself. For my purposes it's OK to NOT to use TAB for Ribbon. For Ribbon I can use KeyTip(s), so basically I have just added Focusable="False" KeyboardNavigation.TabNavigation="None" into ribbon definition. So the whole code could look like this:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
Title="MainWindow" Height="350" Width="525" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="136" />
<RowDefinition Height="175*" />
</Grid.RowDefinitions>
<my:Ribbon Focusable="False" KeyboardNavigation.TabNavigation="None" HorizontalAlignment="Stretch" Name="ribbon1" VerticalAlignment="Top">
<my:RibbonTab Header="Ribbon" KeyTip="R">
<my:RibbonGroup>
<my:RibbonComboBox KeyTip="C" Label="ComboBox" Name="ribbonComboBox1">
<my:RibbonGallery MaxColumnCount="1">
<my:RibbonGalleryCategory>
<my:RibbonGalleryItem Content="An item" />
</my:RibbonGalleryCategory>
</my:RibbonGallery>
</my:RibbonComboBox>
</my:RibbonGroup>
</my:RibbonTab>
</my:Ribbon>
<TextBox TabIndex="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="12,19,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox TabIndex="2" Height="23" HorizontalAlignment="Left" Margin="12,48,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Grid.Row="1" />
</Grid>
</Window>
KeyboardNavigation.TabNavigation="Continue" does it for me, using System.Windows.Controls.Ribbon.dll