WPF: proportional resize of two buttons - wpf

I have a WPF form with two buttons in it. When the window is resized, each of the buttons should occupy half of the window's space at all times.
I can not get it to work.
How is it done?
<Window x:Class="ExampleWin.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:ExampleWin"
mc:Ignorable="d"
Title="Window" Height="200" Width="200" ToolTip="Tooltip" Topmost="True" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip">
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" UseLayoutRounding="True">
<Grid>
<Label x:Name="Backdrop" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
<Button x:Name="Button1" Content="" Margin="1,1,99,1" BorderThickness="0" Background="#FF3B87BD"/>
<Button x:Name="Button2" Content="" Margin="99,1,1,1" BorderThickness="0" Background="#FF59B483"/>
</Grid>
</Border>

add ColumnDefinitions to Grid and make them fill available space proportionally (Width="*" which is a default value)
put Button1 into 1st column (index=0) and Button2 into 2nd, using attached property Grid.Column. update Margin value if you don't need empty space near buttons
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label x:Name="Backdrop" Grid.ColumnSpan="2" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
<Button x:Name="Button1" Grid.Column="0" Content="" Margin="1" BorderThickness="0" Background="#FF3B87BD"/>
<Button x:Name="Button2" Grid.Column="1" Content="" Margin="1" BorderThickness="0" Background="#FF59B483"/>
</Grid>

Related

Change the size of checkbox rectangle without changing its content

In my following WPF app, how can we change the size of the CheckBox without changing the size of its content (an Segoe MDL2 Assets icon). I would like to see the checkbox rectangle to be the half the size of its content (an icon). If I set the width and height of the checkbox to 10 it reduces the content size (icon) as well:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel Margin="10">
<Label FontWeight="Bold">Application Options</Label>
<CheckBox VerticalContentAlignment="Center">
<TextBlock Text="" FontFamily="Segoe MDL2 Assets" FontSize="20"/>
</CheckBox>
</StackPanel>
</Grid>
</Window>
Display of the above XAML:
You could move the TextBlock out of the CheckBox. Something like this:
<StackPanel Margin="10">
<Label FontWeight="Bold">Application Options</Label>
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox RenderTransformOrigin="0.5,0.5" VerticalAlignment="Center">
<CheckBox.RenderTransform>
<ScaleTransform ScaleX="0.5" ScaleY="0.5" />
</CheckBox.RenderTransform>
</CheckBox>
<TextBlock Grid.Column="1" Text="" FontFamily="Segoe MDL2 Assets" FontSize="20"/>
</Grid>
</StackPanel>

Textbox does not show a verticalscrollbar

<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"
mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="550" MinHeight="350">
<DockPanel Background="BlanchedAlmond">
<DataGrid Background="YellowGreen" DockPanel.Dock="Top" MinHeight="100">
</DataGrid>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" FlowDirection="RightToLeft" Margin="10" Background="YellowGreen">
<Button HorizontalAlignment="Right" Click="Button1Add_Click" Margin="5,0">Add text 1</Button>
<Button HorizontalAlignment="Right" Click="Button2Add_Click">Add text 2</Button>
<Button HorizontalAlignment="Right" Click="Button1_Click" Margin="5,0">Toggle textbox 1</Button>
<Button HorizontalAlignment="Right" Click="Button2_Click">Toggle textbox 2</Button>
</StackPanel>
<Grid Background="Red">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBox x:Name="TextBox1" Grid.Row="0" Background="AliceBlue" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">Test 1</TextBox>
<TextBox x:Name="TextBox2" Grid.Row="1" Background="AliceBlue" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">Test 2</TextBox>
</Grid>
</DockPanel>
</Window>
In the example above I am trying to achieve that the two textboxes in the middle just fill the available space between the top datagrid and the bottom stackpanel with buttons.
They have to divide that space between them depending on their text-content and their visibility.
Both properties can change by databindings, I simulated that with the click events.
But when the space is filled up they have to show a vertical scrollbar when needed.
The code sample above is not good. When I add text to a textbox, the grid-row becomes larger but disappears from the visible region, no scroll bar.
EDIT: I ended up with:
<ScrollViewer Background="AliceBlue" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical">
<Label x:Name="Label1" Margin="0,6,0,0">Test 1:</Label>
<TextBox x:Name="TextBox1" Padding="0,6"></TextBox>
<Label x:Name="Label2" Margin="0,6,0,0">Test 2:</Label>
<TextBox x:Name="TextBox2" Padding="0,6" ></TextBox>
</StackPanel>
</ScrollViewer>
You need to add a ScrollViewer in your XAML around the content.
ScrollViewer on MSDN
<ScrollViewer>
Content
</ScrollViewer>

Button inside a custom skewed window doesn't allways get mouseEnter event

Here is my code:
<Window x:Class="WPFStackOverFlow.SkewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True" WindowStyle="None" Background="Transparent"
Title="MainWindow" Height="600" Width="687">
<Border BorderBrush="Green" BorderThickness="2" Background="White" Width="360" Height="360">
<Border.RenderTransform>
<SkewTransform AngleX="-23" AngleY="10"></SkewTransform>
</Border.RenderTransform>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="23" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="X" Width="23" Name="button1" HorizontalAlignment="Right" Click="button1_Click" Height="23" VerticalAlignment="Bottom"/>
<Grid Grid.Row="1">
<Button Height="100" Width="200"></Button>
<TextBlock Text="Some very very long Text" HorizontalAlignment="Center"/>
</Grid>
</Grid>
</Border>
When I move the mouse cursor over the button sometimes it get focus and sometimes it doesn't. How can I make the button inside this custom window to be like a button in a regular window?
I don't think you can apply transformations to windows.
You can apply them to the controls inside windows.
If you need to simulate transformations to windows you can try something similar to what is described in the answer to this question.

WPF Change TabIndex to not follow visual appearence order

I am trying to set the tab index to follow against the order of visual appearance. Meaning the buttons that appears at the top of the window gets focused first when I specifically set it not to.
Here's how the controls are structured;
DocPanel
|
|---- DockPanel
| |----- Button
| |----- Button
| |----- Button
|
|---- Grid
|----- Canvas
|---- TabControl
|------ TextBox
|------ ComboBox
The Tab Order I want;
Canvas
TextBox
ComboBox
3 buttons
Currently order is;
3 buttons
TextBox,
ComboBox
Canvas.
I tried setting KeyboardNavigation.TabNavigation="Local" for the outer DockPanel.
Then I set TabNavigation.TabIndex and TabIndex to the number that I want but that's not working.
If controls appear visually at the top of windows, is it not possible to change the tab index to focus after the controls appearing at the bottom?
Here's my XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusManager.FocusedElement="{Binding ElementName=pic}"
Title="Window1" Height="504" Width="929">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" KeyboardNavigation.TabNavigation="Local">
<DockPanel DockPanel.Dock="Top" Height="30">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Save and Close" KeyboardNavigation.TabIndex="4" TabIndex="4"/>
<Button Content="Forward" KeyboardNavigation.TabIndex="5" TabIndex="5" />
<Button Content="Delete" KeyboardNavigation.TabIndex="6" TabIndex="6" />
</StackPanel>
</DockPanel>
<Grid DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="50"/>
<ColumnDefinition MinWidth="500"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="Aqua" BorderThickness="2" >
<Canvas x:Name="pic" Grid.Column="0" Grid.Row="0" KeyboardNavigation.TabIndex="1" KeyboardNavigation.IsTabStop="True" Focusable="True" >
<Canvas.Background>
<ImageBrush ImageSource="bookcover.jpg" Stretch="Fill"/>
</Canvas.Background>
</Canvas>
</Border>
<TabControl x:Name="tabs" Grid.Column="2" Grid.Row="0">
<TabItem Header="Fax Details" IsTabStop="False">
<StackPanel>
<TextBox Name="fdCustomerFileNumber" HorizontalAlignment="Left" Height="30" KeyboardNavigation.TabIndex="2" TabIndex="2" />
<ComboBox TabIndex="3" KeyboardNavigation.TabIndex="3" Width="165" HorizontalAlignment="Left" Height="22" VerticalAlignment="Center" Name="fdDocType" IsEditable="False" />
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</DockPanel>
You can set your XAML to;
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusManager.FocusedElement="{Binding ElementName=pic}"
Title="Window1" Height="504" Width="929">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<DockPanel DockPanel.Dock="Top" Height="30">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Save and Close" TabIndex="4"/>
<Button Content="Forward" TabIndex="5" />
<Button Content="Delete" TabIndex="6" />
</StackPanel>
</DockPanel>
<Grid DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="50"/>
<ColumnDefinition MinWidth="500"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="Aqua" BorderThickness="2" >
<Canvas x:Name="pic" Grid.Column="0" Grid.Row="0" Focusable="True" >
<Canvas.Background>
<ImageBrush ImageSource="bookcover.jpg" Stretch="Fill"/>
</Canvas.Background>
</Canvas>
</Border>
<TabControl x:Name="tabs" Grid.Column="2" Grid.Row="0">
<TabItem Header="Fax Details" IsTabStop="False">
<StackPanel>
<TextBox Name="fdCustomerFileNumber" HorizontalAlignment="Left" Height="30" TabIndex="2" />
<ComboBox TabIndex="3" Width="165" HorizontalAlignment="Left" Height="22" VerticalAlignment="Center" Name="fdDocType" IsEditable="False" />
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</DockPanel>
</Window>
Then in your code behind, in the New Sub, simply set the focus to the Canvas;
pic.Focus;

WPF control positioning problem

i am learning WPF here is my XAML.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<StackPanel>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</StackPanel>
button textbox is showing but the problem is I am not being able to drag the control to another location. how to fix it. please help. thanks
If you mean you can't drag the Button controls to different locations, it's because they're contained within a StackPanel - stacking them one on top of each other.
If you change that StackPanel to be a Grid, you'd have the ability to drag it around in a canvas-like manner.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<Grid>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</Grid>
This question may shed some light on where to use Grids and StackPanels.
If by "able to drag the control to another location" you are talking about repositioning the control using Expression Blend or the Visual Studio Designer you need to change the StackPanel to a Grid
So it would become-
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="634">
<Grid>
<Button Height="35"
Width="89"
Name="p1">Hello</Button>
<Border CornerRadius="5"
BorderThickness="1"
BorderBrush="Black"
Height="35"
Width="254"
Margin="91,192,150,79">
<TextBox HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
Height="35"
Width="250"
Name="txtContents" />
</Border>
<Button Height="23"
Name="button1"
Width="75">Button</Button>
</Grid>

Resources