How to add scroll in a textBox in windows 8 app? - c#

I want to add a scroll viewer in a textBOX in a windows 8 app. So that user can scroll through his long text

Add a scrollviewer into the XAML. Then set the margins of the scrollviewer by cutting and pasting that of the textbox, and set the height and width of textbox to auto.

I just rewrote what Harshit explained and added some pics and Code.
Add a scrollviewer to your XAML
Select Margin in Scrollviewer (Best way: Click on the TestViewer-Code in the XAML to select it)
Set the Margin Values to Auto
Paste (in XAML) the Textbox and paste it in the Scrollviewer-Tag
Set the Height and width of Textbox to Auto
done!
XAML:
<Page
x:Class="testapp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:newcalapp_winrt"
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}" Margin="0,4,0,-4">
<Button Click="showText" Content="ShowText" x:Name="btn" Width="200" Height="56" Margin="1037,620,0,92"></Button>
<ScrollViewer x:Name="outputTextBoxScrollViewer" Margin="57,200,700,400">
<TextBox x:Name="outputTextBox" AcceptsReturn="True"/>
</ScrollViewer>
<ScrollViewer x:Name="outputTextBlockScrollViewer" Margin="57,450,700,169">
<TextBlock x:Name="outputTextBlock"/>
</ScrollViewer>
</Grid>
</Page>
C#:
namespace testapp
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
void showText(object sender, RoutedEventArgs args)
{
//OutputString
String outputString;
//Random number
Random randomizer = new Random();
int randomNumber = randomizer.Next(0,100000);
//Some magic with Dates :) Not important!
...
outputTextBox.Text = outputString;
outputTextBlock.Text = outputString;
}
}
}
http://i.stack.imgur.com/2qDyJ.png">

Related

WinUI3: Binding Text Block width to width of a grid column in code-behind

So, I want to bind the width of my text block to the actual width of a grid column. The text block is generated in the code behind so the binding must be declared there as well.
The binding code is as follows:
Binding binding = new Binding();
binding.Source = pageWidth;
binding.Path = new PropertyPath("ActualWidth");
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(textBlock, TextBlock.WidthProperty, binding);
textBlock is my text and pageWidth is just a column definition. When I run the application, the text block doesn't appear. I'm assuming the binding is setting the width to 0.
Using Debug.WriteLine(pageWidth.ActualWidth); I can see that when the binding is declared the actual width of pageWidth is 0, but that's just because it hasn't been rendered yet, since it's binded surely the text block's width should update when the pageWidth is rendered?
I also tried establishing the binding using textBlock.SetBinding(TextBlock.WidthProperty, binding); as well but still get the same result.
Any help would be appreciated :)
You don't need to binding for this. The TextBox should extend itself.
This code works:
MainPage.xaml
<Page
x:Class="BindingTests.MainPage"
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:local="using:BindingTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Grid x:Name="RootGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</Page>
MainPage.xaml.cs
using Microsoft.UI.Xaml.Controls;
namespace BindingTests;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
TextBlock textBlock1 = new()
{
Text = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20",
};
Grid.SetColumn(textBlock1, 0);
this.RootGrid.Children.Add(textBlock1);
TextBlock textBlock2 = new()
{
Text = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z",
};
Grid.SetColumn(textBlock2, 1);
this.RootGrid.Children.Add(textBlock2);
}
}

How to add some of my code to xaml property ResizeMode= CanResizeWithGrip or whenever it is called

Hi at the begining of this question i want to say that i'm beginer and thing i've done here could prolly be done better but i'm still learnig. i want to set a value of some variable that contains the actual width of grid, and whenever i resize my window obviously this width change and I don't know how to access it. What i want to do ? Just want my progress bar to scale up or down when resizing
Here is some code
<Window x:Class="Lista6.MainPage"
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:Lista6"
mc:Ignorable="d"
Title="MainPage" Height="650" Width="1000"
Background="#36393F"
ResizeMode="CanResizeWithGrip"
Icon="/img/Ikonka2.png"
WindowStyle="None">
///other xaml code
<ProgressBar Foreground="White"
Value="50"
BorderThickness="0"
Background="Gray"
Height="5"
Width="{Binding Path=ProgresBarWidth[0], Mode=OneWay}"/>
</StackPanel>
</Grid>
</Grid>
</Window>
ObservableCollection<double> width = new ObservableCollection<double> { };
public ObservableCollection<double> ProgresBarWidth
{
get
{
return width;
}
set
{
width = value;
}
}
public AppOperator()
{
width.Add(0);
}
private void ProgresbarWidthSetter()
{
MainPage mp = new MainPage();
width[0] = mp.MusicBar.Width;
}
Here as u can see i ve binded progres bar width to ObservColl property, and i want to call it when im resizing my window but i ve no idea how to do that.
Thanks for any help and wish u all good.

WPF Set DataTemplate Grid size in code behind (ResourceDictionary)

I have ResourceDictionary that holds Dialog template. It has own DataType="{x:Type viewModels:DialogViewModel}". I would like to set this dialog "window" size on initialization. I know how to do it if I will add for example Height property to DialogViewModel. However this is not the right place to specify Height. How to do it in code behind and then Bind to that property? I think I have tried all the possible solutions I was able to find.
Basically I need to specify Height in SplitDialog.xaml.cs, let's say Height=500 and then add it to <Grid Margin="20,20,20,10" Tag="Category dialog" MinHeight="450" Height="???" x:Name="MainGrid">, but how?
I have tried to add to SplitDialog.xaml.cs (returns Height is null):
Grid gGrid = (Grid)this.FindName("MainGrid");
gGrid.Height = 600;
SplitDialog.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Controls.Styles.Dialog.SplitDialog"
xmlns:viewModels="clr-namespace:ViewModels.Category;assembly=ViewModels">
<DataTemplate DataType="{x:Type viewModels:DialogViewModel}">
<DataTemplate.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</ResourceDictionary>
</DataTemplate.Resources>
<Grid Margin="20,20,20,10" Tag="Category dialog" MinHeight="450" x:Name="MainGrid">
</Grid>
</DataTemplate>
</ResourceDictionary>
SplitDialog.xaml.cs:
public partial class SplitDialog : ResourceDictionary
{
public SplitDialog()
{
}
}
Provide this logic in Loaded event of the Grid, e.g:
<Grid Loaded="Grid_Loaded">...
then in code-behind:
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
Grid grid = sender as Grid;
if (grid != null)
{
grid.Height = 600;
}
}
no?

How to show images to fullscreen in Windows Phone 8? [duplicate]

I am working on Windows Phone 8 app and have a Image view like this in XAML:
<Image Name="Image"
Grid.Row="0"
Visibility="Collapsed"
Width="Auto"
Height="Auto"
Tap="Image_tap"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="1,1,1,1"/>
Now i have this event called Tap="Image_tap", when i tap on the image i want to show the same image in full screen without any bar on top and bottom, how to acheive this ?
An alternative approach, without passing the image details between pages, is to display a Popup:
private void HandleTapImage(object sender, GestureEventArgs e)
{
var myPopup = new Popup
{
Child = new Image
{
Source = ((Image) sender).Source,
Stretch = Stretch.UniformToFill,
Height = Application.Current.Host.Content.ActualHeight,
Width = Application.Current.Host.Content.ActualWidth
}
};
myPopup.IsOpen = true;
}
(Select Strech value best suited for your needs).
With this approach however you have to manually hide ApplicationBar and SystemTray, if present, in the HandleTapImage method. You also have to take care of hiding the Popup and showing the bars again.
Bottom bar is ApplicationBar and top bar is SystemTray. If you create a new page without the ApplicationBar and with SystemTray.IsVisible to false, you have a fullscreen page. Now, instead of having a Grid at the root, place just one Image and you can use that page as a fullscreen viewer.
<phone:PhoneApplicationPage
x:Class="SimpleApp.FullScreenPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="False">
<Image Name="myImage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Uniform"/>
</phone:PhoneApplicationPage>
In MainPage where you tap image:
private void myImg_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
string context = ((sender as Image).Source as BitmapImage).UriSource.ToString();
NavigationService.Navigate(new Uri(String.Concat("/Page1.xaml?context=", context), UriKind.RelativeOrAbsolute));
}
In Fullscreenpage:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string context = this.NavigationContext.QueryString["context"];
myImage.Source = new BitmapImage(new Uri(context, UriKind.RelativeOrAbsolute));
base.OnNavigatedTo(e);
}

Show image in Full screen

I am working on Windows Phone 8 app and have a Image view like this in XAML:
<Image Name="Image"
Grid.Row="0"
Visibility="Collapsed"
Width="Auto"
Height="Auto"
Tap="Image_tap"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="1,1,1,1"/>
Now i have this event called Tap="Image_tap", when i tap on the image i want to show the same image in full screen without any bar on top and bottom, how to acheive this ?
An alternative approach, without passing the image details between pages, is to display a Popup:
private void HandleTapImage(object sender, GestureEventArgs e)
{
var myPopup = new Popup
{
Child = new Image
{
Source = ((Image) sender).Source,
Stretch = Stretch.UniformToFill,
Height = Application.Current.Host.Content.ActualHeight,
Width = Application.Current.Host.Content.ActualWidth
}
};
myPopup.IsOpen = true;
}
(Select Strech value best suited for your needs).
With this approach however you have to manually hide ApplicationBar and SystemTray, if present, in the HandleTapImage method. You also have to take care of hiding the Popup and showing the bars again.
Bottom bar is ApplicationBar and top bar is SystemTray. If you create a new page without the ApplicationBar and with SystemTray.IsVisible to false, you have a fullscreen page. Now, instead of having a Grid at the root, place just one Image and you can use that page as a fullscreen viewer.
<phone:PhoneApplicationPage
x:Class="SimpleApp.FullScreenPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="False">
<Image Name="myImage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stretch="Uniform"/>
</phone:PhoneApplicationPage>
In MainPage where you tap image:
private void myImg_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
string context = ((sender as Image).Source as BitmapImage).UriSource.ToString();
NavigationService.Navigate(new Uri(String.Concat("/Page1.xaml?context=", context), UriKind.RelativeOrAbsolute));
}
In Fullscreenpage:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string context = this.NavigationContext.QueryString["context"];
myImage.Source = new BitmapImage(new Uri(context, UriKind.RelativeOrAbsolute));
base.OnNavigatedTo(e);
}

Categories