How to align button on stackpanel in windows phone 7? - silverlight

i have added two buttons in stackpanel and set alignment as shown in the code
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<Button Content="Button" Height="64" Name="button1" Width="160" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Button" Height="64" Name="button2" Width="160" HorizontalAlignment="Right" VerticalAlignment="Top"/>
</StackPanel>
but this doesn't match with my requirement. I want it to be like shown in image below.
So how can i do this?

Something like this:
<Grid
Width="480">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*" />
<ColumnDefinition
Width="*" />
</Grid.ColumnDefinitions>
<Button
Width="200"
Content="Clear" />
<Button
Grid.Column="1"
Width="200"
Content="Options"
HorizontalAlignment="Right"
/>
</Grid>
UPDATE: Due to popular demand, here is a grid without the fixed width or the columns.
<Grid>
<Button
Width="150"
Content="Clear"
HorizontalAlignment="Left"
/>
<Button
Width="150"
Content="Options"
HorizontalAlignment="Right"
/>
</Grid>

Related

Make content inside Button responsive in WPF

I have vertical navigation bar. Each button of the navigation has it's own image and text. For now the image has static height set to Height="50"and text has the default value. When I resize the window the buttons grow responsive but the content inside keeps the same size.
This is how it look right now:
https://gyazo.com/48cff48437b66f462dccd23639dd59ac (GIF)
My XAML code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<UniformGrid Background="DodgerBlue" Grid.Column="1" Columns="1">
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/home-5-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Home</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/search-3-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Search</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/twitter-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Twitter</TextBlock>
</StackPanel>
</Button>
<Button BorderThickness="0" Background="DodgerBlue">
<StackPanel>
<Image Source="Icons/chat-4-xxl.png" Height="50" Margin="0,0,0,10"/>
<TextBlock HorizontalAlignment="Center" Foreground="White" FontWeight="Bold">Chat</TextBlock>
</StackPanel>
</Button>
</UniformGrid>
</Grid>
How could i make content inside the Buttons responsive and grow when the button grows in XAML WPF?
Use Viewbox as shown here.
<Viewbox Stretch="Uniform">
<Button ... />
</Viewbox>

How to keep elements in ScrollViewer in sight, regardless of scrolling

In Silverlight, is it possible to let certain elements inside a ScrollViewer stay in view? By this I mean that certain elements should always be visible, regardless of where the scroll position currently is.
Take this XAML for example (you can put it in something like KaXaml of XamlPad):
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ScrollViewer Width="250" Height="100" HorizontalScrollBarVisibility="Visible">
<StackPanel>
<Grid Width="350" Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Button Content="1" Grid.Column="0" BorderThickness="2" Width="50" />
<Button Content="2" Grid.Column="1" BorderThickness="2" Width="50" />
<Button Content="3" Grid.Column="2" BorderThickness="2" Width="50" />
<Button Content="4" Grid.Column="3" BorderThickness="2" Width="50" />
<Button Content="5" Grid.Column="4" BorderThickness="2" Width="50" />
<Button Content="6" Grid.Column="5" BorderThickness="2" Width="50" />
<Button Content="X" Grid.Column="6" BorderThickness="2" Width="50" />
</Grid>
<Grid Width="350" Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Button Content="1" Grid.Column="0" BorderThickness="2" Width="50" />
<Button Content="2" Grid.Column="1" BorderThickness="2" Width="50" />
<Button Content="3" Grid.Column="2" BorderThickness="2" Width="50" />
<Button Content="4" Grid.Column="3" BorderThickness="2" Width="50" />
<Button Content="5" Grid.Column="4" BorderThickness="2" Width="50" />
<Button Content="6" Grid.Column="5" BorderThickness="2" Width="50" />
<Button Content="X" Grid.Column="6" BorderThickness="2" Width="50" />
</Grid>
</StackPanel>
</ScrollViewer>
</Grid>
</Page>
I would like to keep the 'X' buttons in sight, regardless of how much you scroll left or right. Of course, when scrolling down or up, the 'X' buttons should follow the scrolling.
I'm not saying the structure of my XAML is ideal, but each row is a databound to a different item, so each row is a data template. I can't put one big grid in the scrollviewer or have columns instead of rows as templates (at least, I think I can't).
Ya no sweat, just layer those elements over your Scrollviewer in a Panel like this;
<Grid>
<ScrollViewer>
... Scroll Viewer Embedded Stuff ...
</ScrollViewer>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5">
<Button Content="Button1"/>
<Button Content="Button1"/>
<Button Content="Button1"/>
<Button Content="Button1"/>
<Button Content="Button1"/>
</StackPanel>
</Grid>
You could even go a step further and set a EventTrigger to make your buttons visible on MouseEnter and disappear on MouseLeave if you wanted but this should get you started. Best of luck!
You might have more luck using a DataGrid and have the X buttons as a frozen column.

KeyboardNavigation.TabNavigation="Once" on Toolbar does not lose focus unless all elemets are contained in another container

In the XAML below, the first toolbar (Toolbar1 below) does not lose focus and traverses all elements even with KeyboardNavigation.TabNavigation="Once" set. The only way I have make it work is by putting all elements of the toolbar into another container such as stack panel or grid (Toolbar2 below). However, then overflow functionality does not work appropriately. When width is reduced, whole inside container overflows instead of individual items.
My requirement is to tab out from the toolbar after first element (for remaining items user will use the arrow keys) without losing the overflow functionality of overflowing individual items as needed.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="100" />
<RowDefinition Height="50" />
<RowDefinition Height="100" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="First" Width="600"></TextBox>
<TextBox Grid.Row="1" Text="Second" Width="600"></TextBox>
<DockPanel LastChildFill="True" Grid.Row="2" Background="Red" >
<ToolBar x:Name="Toolbar1" DockPanel.Dock="Top"
VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="40" BandIndex="1" Band="1"
KeyboardNavigation.TabNavigation="Once" >
<Button Background="Red" Content="1" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
<Button Background="Red" Content="2" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
<Button Background="Red" Content="3" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
<Button Background="Red" Content="4" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
<Button Background="Red" Content="5" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
</ToolBar>
<TextBox DockPanel.Dock="Bottom" Text="I'm first Rich Text Box, My toolbar is not in a inner container." Height="50" Width="600"></TextBox>
</DockPanel>
<TextBox Grid.Row="3" Text="Third" Width="600"></TextBox>
<DockPanel LastChildFill="True" Grid.Row="4" Background="Green">
<ToolBar x:Name="Toolbar2" DockPanel.Dock="Top"
VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="40" BandIndex="1" Band="1"
KeyboardNavigation.TabNavigation="Once" >
<StackPanel Orientation="Horizontal" KeyboardNavigation.TabNavigation="Once" >
<Button Background="Green" Content="1" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded" />
<Button Background="Green" Content="2" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded"/>
<Button Background="Green" Content="3" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded"/>
<Button Background="Green" Content="4" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded"/>
<Button Background="Green" Content="5" Height="28" Width="28" ToolBar.OverflowMode="AsNeeded"/>
</StackPanel>
</ToolBar>
<TextBox DockPanel.Dock="Bottom" Text="I'm second Rich Text Box, My toolbar is in a inner container." Height="50" Width="600"></TextBox>
</DockPanel>
<TextBox Grid.Row="5" Text="Fourth" Width="600"></TextBox>
</Grid>
As I get correct you should turn off FocusManager.IsFocusScope attached property for your ToolBar1, don't I?

automatic alignment of buttons

I got 4 buttons aligned inside a grid. Is there any way to make alignment automatic so that if one button's visibility is set to false the remaining buttons uses the space wisely[other buttons moves to accommodate into new space]?
Instead of a grid, put the buttons in a StackPanel with Orientation="Horizontal". Also, make sure you set your button Visibility to "Collapsed" instead of "Hidden".
Try the following:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal">
<Button Content="1" Width="50" Height="23" Visibility="Collapsed"/>
<Button Content="2" Width="50" Height="23"/>
<Button Content="3" Width="50" Height="23" Visibility="Collapsed"/>
<Button Content="4" Width="50" Height="23"/>
</StackPanel>
</Grid>
Or this:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="1" Content="1" Width="50" Height="23" Visibility="Collapsed"/>
<Button Grid.Column="2" Content="2" Width="50" Height="23"/>
<Button Grid.Column="3" Content="3" Width="50" Height="23" Visibility="Collapsed"/>
<Button Grid.Column="4" Content="4" Width="50" Height="23"/>
</Grid>
You should see the buttons centered but only show the second and fourth button
Depends on the layout and how complex you want the space distribution to be, if a uniform distribution is enough you could use a UniformGrid, in a normal Grid that would be a bit tricky as the columns and rows need to be adjusted.

WP7 Max width textbox and position to set from right

I have this code:
<controls:PivotItem Header="Hledat">
<Canvas Margin="0">
<TextBox Name="SearchTxb" Height="72" Text="" VerticalAlignment="Top" HorizontalAlignment="Right" />
<Button Name="SearchBtn" Margin="0,74,100,0" Width="200" Height="72" Click="SearchBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Top">Hledat</Button>
</Canvas>
</controls:PivotItem>
and I want to support Portrait and Landscape. This should be pivotitem for search. I want to textbox to change width to full size of phone screen and I want to button set position to f.e. 20 from right side. What should I add to code? Thanks
<controls:PivotItem Header="Hledat">
<Grid Margin="0">
<Grid.ColumnDefinition>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinition>
<TextBox Grid.Column="0" Name="SearchTxb" Height="72" Text="" VerticalAlignment="Top" HorizontalAlignment="Stretch" />
<Button Grid.Column="1" Name="SearchBtn" Margin="0,74,100,0" Width="200" Height="72" Click="SearchBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Top">Hledat</Button>
</Grid>
</controls:PivotItem>

Resources