Resizing a control in Windows store app - windows-8

Was just wondering how I would go about letting the user resize a TextBox control at runtime by dragging its corners in Windows Store App. Less importantly, is the same technique used for the resizing of all controls?
Thank and regards!

Here I am giving you only for textbox as for others it's same.
XAML Code
<Page>
<Canvas Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="grdTextbox" Canvas.Left="300" Canvas.Top="300" Height="40" Width="200">
<Thumb x:Name="ThumbBottomRight" Background="White" Height="10" Width="10" HorizontalAlignment="Right" DragDelta="ThumbBottomRight_DragDelta" VerticalAlignment="Bottom"/>
<Thumb x:Name="ThumbBottomLeft" Background="White" Height="10" Width="10" HorizontalAlignment="Left" DragDelta="ThumbBottomLeft_DragDelta" VerticalAlignment="Bottom"/>
<Thumb x:Name="ThumbTopRight" Background="White" Height="10" Width="10" HorizontalAlignment="Right" DragDelta="ThumbTopRight_DragDelta" VerticalAlignment="Top"/>
<Thumb x:Name="ThumbTopLeft" Background="White" Height="10" Width="10" HorizontalAlignment="Left" DragDelta="ThumbTopLeft_DragDelta" VerticalAlignment="Top"/>
<TextBox Margin="5" Text="This is resizable textbox"/>
</Grid>
</Canvas>
</Page>
C# Code
private void ThumbTopLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
grdTextbox.Width -= e.HorizontalChange;
grdTextbox.Height -= e.VerticalChange;
Canvas.SetLeft(grdTextbox, Canvas.GetLeft(grdTextbox) + e.HorizontalChange);
Canvas.SetTop(grdTextbox, Canvas.GetTop(grdTextbox) + e.VerticalChange);
}
private void ThumbTopRight_DragDelta(object sender, DragDeltaEventArgs e)
{
grdTextbox.Width += e.HorizontalChange;
grdTextbox.Height -= e.VerticalChange;
Canvas.SetTop(grdTextbox, Canvas.GetTop(grdTextbox) + e.VerticalChange);
}
private void ThumbBottomLeft_DragDelta(object sender, DragDeltaEventArgs e)
{
grdTextbox.Width -= e.HorizontalChange;
grdTextbox.Height += e.VerticalChange;
Canvas.SetLeft(grdTextbox, Canvas.GetLeft(grdTextbox) + e.HorizontalChange);
}
private void ThumbBottomRight_DragDelta(object sender, DragDeltaEventArgs e)
{
grdTextbox.Width += e.HorizontalChange;
grdTextbox.Height += e.VerticalChange;
}

Related

Dynamically add and remove controls, using uwp windows 10 visual studio 2015

Edit:23-10;15:37h
Lets make one very big story very small. I've found out a lot!
first impressing of possibible layout
I want a program, were I can place/replace/rotate/adjust/remove fences in a field.
At this point I drag the fences from the listview into the canvas. But now I have a problem. I can't give those a name. I can't find a way to interact.
And seeing that I have come this far. Perhaps it's best to consider a save/load option. I expect some sort of csv of xml file could be used.
xaml.cs
byte Selectie = 0;
private void parcours_Drop(object sender, DragEventArgs e)
{
var pointer = e.GetPosition(parcours);
//following lines are used to create an object.
if (Selectie !=0)
{
Image enkel = new Image();
enkel.Width = 70;
enkel.Height = 70;
if (Selectie == 1)
{
enkel.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/hindernissen_01.png"));
}
if (Selectie == 2)
{
enkel.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/hindernissen_02.png"));
}
if (Selectie == 3)
{
enkel.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/hindernissen_03.png"));
}
enkel.CanDrag = true;
parcours.Children.Add(enkel);
Canvas.SetTop(enkel, pointer.Y - 35);
Canvas.SetLeft(enkel, pointer.X - 35);
}
if (Selectie == 0)
{
Canvas.SetTop(enkel, pointer.Y - 35);
Canvas.SetLeft(enkel, pointer.X - 35);
}
Selectie = 0;
}
private void parcours_DragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;//you must accept accept a drop. prevents it from dropping anywere.
}
private void tripple_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Selectie = 3;
}
private void dubbel_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Selectie = 2;
}
private void enkel_DragStarting(UIElement sender, DragStartingEventArgs args)
{
Selectie = 1;
}
mainpage
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock x:Name="Header1"
HorizontalAlignment="Left"
Height="59" Margin="118,0,0,0"
TextWrapping="Wrap" Text="Parcourshulp"
VerticalAlignment="Top" Width="840"
Foreground="Black" FontSize="48"
Grid.ColumnSpan="2"
/>
<Rectangle Fill="Red"
Height="18"
Margin="1,64,0,0"
Stroke="Black"
VerticalAlignment="Top"
Grid.ColumnSpan="2"
/>
<Canvas x:Name="parcours" HorizontalAlignment="Left"
Height="306"
Margin="33,176,0,0"
VerticalAlignment="Top"
Width="745"
Background="#FFECE2E2"
AllowDrop="True"
Grid.ColumnSpan="2" Drop="parcours_Drop" DragOver="parcours_DragOver">
<Image x:Name="enkel_Copy2" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="70" RenderTransformOrigin="0.09,-0.015" Source="Assets/hindernissen_01.png" CanDrag="True" Canvas.Left="456" Canvas.Top="195" />
</Canvas>
<ListView x:Name="listView" Background="#FFECE2E2" Height="306" Margin="827,176,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="107" RenderTransformOrigin="0.518,0.532">
<Image x:Name="enkel" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="70" RenderTransformOrigin="0.09,-0.015" Source="Assets/hindernissen_01.png" CanDrag="True" DragStarting="enkel_DragStarting">
</Image>
<Image x:Name="dubbel" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="70" RenderTransformOrigin="0.09,-0.015" Source="Assets/hindernissen_02.png" CanDrag="True" DragStarting="dubbel_DragStarting"/>
<Image x:Name="tripple" HorizontalAlignment="Left" Height="70" VerticalAlignment="Top" Width="70" RenderTransformOrigin="0.09,-0.015" Source="Assets/hindernissen_03.png" CanDrag="True" DragStarting="tripple_DragStarting"/>
</ListView>
</Grid>
</Page>

Xaml simple storyboard/animation

I have a Xaml-view containing three buttons and three usercontrols.
I in the code-behind i have buttons that toggles visibility on or off:
<Grid HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button Content="Summer" Click="ButtonBase_OnClick" HorizontalAlignment="Left"/>
<Button Content="Winter" Click="ButtonBase_OnClick3" HorizontalAlignment="Center"/>
<Button Content="Autumn" Click="ButtonBase_OnClick1" HorizontalAlignment="Right"/>
</StackPanel>
<Grid VerticalAlignment="top" HorizontalAlignment="Center" Margin="0,50,0,0">
<StackPanel Width="600" VerticalAlignment="top" Background="LightGreen">
<local:Summer x:Name="Summer" Width="600"></local:Summer>
<local:Winter x:Name="Winter" Visibility="Collapsed" Width="600"></local:Winter>
<local:Autumn x:Name="Autumn" Visibility="Collapsed" Width="600"></local:Autumn>
</StackPanel>
</Grid>
</Grid>
Code behind:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Autumn.Visibility = Visibility.Collapsed;
Summer.Visibility = Visibility.Collapsed;
Winter.Visibility = Visibility.Visible;
}
private void ButtonBase_OnClick1(object sender, RoutedEventArgs e)
{
Autumn.Visibility = Visibility.Collapsed;
Summer.Visibility = Visibility.Visible;
Winter.Visibility = Visibility.Collapsed;
}
private void ButtonBase_OnClick3(object sender, RoutedEventArgs e)
{
Autumn.Visibility = Visibility.Visible;
Summer.Visibility = Visibility.Collapsed;
Winter.Visibility = Visibility.Collapsed;
}
I would like to know how to accomplish a simple animation (slide in) by using storyboards or other way. I´ve seen examples online but I have had a hard time making them work in my specifik case.

Customized XAML Spinner

I am trying to make a Spinner for a Windows Phone 8.1 App. I want the Spinner to have 2 wheels: A list of numbers on one, and a list of words (not AM/PM) on the other. Something similar to the TimePicker:
I did not see any options that worked this way. (ComboBox is the closest I found, but it does not spin.)
Is there a way to customize the TimePicker? Or create a Spinner like it?
If you look through sources of DatePicker, you'll see them use Microsoft.Phone.Controls.Primitives.LoopingSelector as wheel. It is public class and you can use it from Windows Phone Toolkit.
You also can see example of usage in DatePicker sources.
here's a starting point using a ScrollViewer with VerticalSnapPointsType="Mandatory"
When ScrollViewer.ViewChanged use ScrollViewer.VerticalOffset and the height of your container to calculate the selected item. Sorry Stackpanel does not support itemssource binding so you may have to add the elements in the code behind.
the XAML
<ScrollViewer Width="70"
Loaded="ScrollViewer_OnLoaded"
VerticalContentAlignment="Center"
VerticalSnapPointsType="Mandatory"
VerticalSnapPointsAlignment="Center">
<StackPanel Margin="0,200" x:Name="StackPanel">
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="1"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="2"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="3"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="4"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="5"></TextBlock>
</Border>
</Grid>
<Grid Height="80"
Width="70">
<Border BorderBrush="Aqua"
BorderThickness="1"
Height="74"
Width="70">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
Text="6"></TextBlock>
</Border>
</Grid>
</StackPanel>
</ScrollViewer>
The code behind
private void ScrollViewer_OnLoaded(object sender, RoutedEventArgs e)
{
ScrollViewer sv = sender as ScrollViewer;
sv.ChangeView(0, 200, null, true);
sv.ViewChanged += sv_ViewChanged;
}
void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
if (e.IsIntermediate)
{
return;
}
else
{
ScrollViewer sv = sender as ScrollViewer;
double offset = sv.VerticalOffset;
double stackpanelMargin = 200;
double itemHeight = 80;
int selectedIndex = (int)Math.Round((offset + stackpanelMargin) / itemHeight);
//int selectedIndex = int.Parse(indexOfSelectedItem.ToString());
//get selected item
var StackpanelChildren = StackPanel.Children;
int i = 0;
foreach (var stackpanelChild in StackpanelChildren)
{
if (i == selectedIndex)
{
stackpanelChild.Opacity = 1;
}
else
{
stackpanelChild.Opacity = .5;
}
i++;
}
}
}

Placing PopUp, which is inside a Itemscontrol

I've got an itemscontrol, in which all my elements are PopUps. The problem is I do not know how to place them compared to the grid in which the itemscontrol is placed. I've tried using horizontal- and vertical alignment but this does not help. The xaml code I've got so far is:
<Grid>
<ItemsControl Grid.Row="1" VerticalContentAlignment="Top" HorizontalAlignment="Left" ItemsSource="{Binding PopUp}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
<DataTemplate>
<Popup IsOpen="{Binding PU.IsOpen}" HorizontalAlignment="Left" VerticalAlignment="Top">
<Design:InGame_NextTurn_UserControl/>
</Popup>
</DataTemplate>
</ViewModel:PopUpTemplateSelector.EndTurnMenuTemplate>
</ViewModel:PopUpTemplateSelector>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
I would suggest maybe using the following overlay method instead.
From this Nokia page:
Overlay XMAL:
<UserControl x:Class="WindowsPhoneSample7.OverLay"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
d:DesignHeight="800" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="400"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ProgressBar IsIndeterminate="True" Foreground="Orange" Height="50" Width="480" VerticalAlignment="Center"/>
<TextBlock Text="Wait" Foreground="Orange" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</UserControl>
Overlay code-behind:
public OverLay()
{
InitializeComponent();
this.LayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
this.LayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
SystemTray.IsVisible = false; //to hide system tray
}
MainPage code-behind:
public partial class MainPage : PhoneApplicationPage
{
private Popup popup;
// Constructor
public MainPage()
{
InitializeComponent();
this.popup = new Popup();
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
this.LayoutRoot.Opacity = 0.2;
OverLay ovr = new OverLay();
this.popup.Child = ovr;
this.popup.IsOpen = true;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, a) =>
{
Thread.Sleep(5000);
};
worker.RunWorkerCompleted += (s, a) =>
{
popup.IsOpen = false;
this.LayoutRoot.Opacity = 1.0;
};
worker.RunWorkerAsync();
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
this.popup.IsOpen = false;
}
}

How to use the enter key to submit login details

I have a basic login page,
<TextBox x:Name="UsernameInput" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Username, Mode=TwoWay}" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2" Width="400" />
<PasswordBox HorizontalAlignment="Left" VerticalAlignment="Center" Password="{Binding Password, Mode=TwoWay}" Grid.Row="1" Grid.Column="2" Width="400"/>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Username: " VerticalAlignment="Center" Margin="0,0,0,15" Grid.Row="0" Grid.Column="0" Style="{StaticResource SubheaderTextStyle}"/>
<TextBlock HorizontalAlignment="Right" TextWrapping="Wrap" Text="Password: " VerticalAlignment="Center" Margin="0,0,0,15" Grid.Row="1" Grid.Column="0" Style="{StaticResource SubheaderTextStyle}"/>
<Button Content="Login" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Grid.Column="2" Height="50" Width="300" Command="{Binding LoginCommand}"/>
How can I simulate the "Login" button being pressed if the user hits the Enter key from the password field?
Thanks!
You can use KeyDown event of PasswordBox.
<PasswordBox KeyDown="txtPassword_KeyDown"/>
private void txtPassword_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
//TODO: do login
}
Use the KeyDown event in the PasswordBox -
<PasswordBox KeyDown="PasswordKeyDown"/>
Then in your c# code check if the enter key has been pressed, and log in accordingly:
using System.Windows.Input;
private void PasswordKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
Login();
}
If you want that all input fields will initiate the "Login" button when pressing Enter you can use the Button IsDefault property and set it to True, you can read more HERE.
When set to true, if you press return while some objects in the window have focus, the code of the click event of the button will be fired automatically.
If you want that only PasswordBox will initiate the "Login" button you can add keyDown property in the xaml:
<PasswordBox KeyDown="Password_KeyDown"/>
And in the C# add:
private void Password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
Login();
}