XAML horizontal alignment - xaml

I have a question on Windows Phone 7 XAML programming.
<StackPanel Orientation="Vertical" Width="110">
<Canvas Margin="0">
<TextBlock Text="{Binding Menesis}" Foreground="{Binding MyColor}"></TextBlock>
</Canvas>
<Canvas Margin="0,12,0,0">
<TextBlock Text="{Binding Datums}" Foreground="{Binding MyColor}" FontSize="85" HorizontalAlignment="Center" TextAlignment="Center" />
</Canvas>
<Canvas Margin="0,105,0,0">
<TextBlock Text="{Binding Nedelas_diena}" Foreground="{Binding MyColor}" />
</Canvas>
</StackPanel>
How to make TextBlock (Binding Datums) text centered? Currently it is aligned at right side and
HorizontalAlignment="Center"
or
TextAlignment="Center"
doesn't work.

If you have your TextBlock in a Canvas you will have problems with the alignment as the TextBlack will be placed at 0,0 (top left) inside the canvas control and (unless you set the width explicitly) will be stretched to fit the text contained. This basically means your text will always be left aligned.
The Canvas control should only be used when you need to set the exact position of the contained elements. If this is not the case then use another container such as a Grid, StackPanel or even just a ContentControl.
Remove the Canvas from your xaml and it should work.
To clarify HorizontalAlignment vs. TextAlignment:
If you have a ContentControl that is 400px wide and you add a TextBlock to it that is set to be 200px wide with text content that is 100px wide, then the following is true:
Setting the HorizontalAlignment to Center will align the TextBlock (200px wide) to the middle of the ContentControl but the text will still be left aligned within the TextBlock. This means the text will be offset 100px from the left.
If just the TextAlignment is set to Center then the TextBlock will be left-aligned but the text inside will be centered. This means that the text will be offset 50px from the left.
In my opinion the best practise here is not to set any widths and just set the TextAlignment property. This will mean (for most containers) the TextBlock will be stretched the entire width of the container and the text within aligned appropriately.

Instead of using stack panel and canvas . either u can use Grid. If you are using grid you can point out the exact position where you need to place the control.
for eg,
<----- Height -->
like this. More over there is no need to use canvas. The canvas container is using mainly in case of game design purpose.

Related

How to resize NavigationView and SplitView in UWP

I want to enable user to resize the NavigationView in my UWP app. Couldn't find any resources how I could do that.
I see some of the apps have "Resizable splitView" but for SplitView also, I cannot see any such resize property exposed to set by default.
Pls help.
Thanks in Advance
There are no such property can resize SplitView and NavigationView, you need to custom layout to implement a similar effect. You could use Slider control and bind the OpenPaneLength Property of SplitView to Slider.Value to do this. Please refer to the following code.
<Grid>
<SplitView Name="CoreSplitView"
DisplayMode="Inline"
IsPaneOpen="True"
OpenPaneLength="{Binding Value, ElementName=MasterSlider, Mode=OneWay}">
<SplitView.Pane>
<Grid Name="PaneGrid" Background="Gray">
<Slider Name="MasterSlider"
MinWidth="480"
VerticalAlignment="Stretch"
Maximum="480"
Minimum="10"
Opacity="0"
Value="150"
/>
<StackPanel Name="PaneStackPanel"
Margin="0,0,10,0" Background="LightGray">
<TextBlock TextWrapping="Wrap" Text="Use a slider when you want your users to be able to set defined, contiguous values (such as volume or brightness) or a range of discrete values (such as screen resolution settings).A slider is a good choice when you know that users think of the value as a relative quantity" />
</StackPanel>
</Grid>
</SplitView.Pane>
<Grid Name="ContentGrid" Background="LightSteelBlue">
</Grid>
</SplitView>
</Grid>
The StackPanel that is directly under our slider, acts like a cover for our Slider. Notice the StackPanel has Margin="0,0,10,0", this translates to a 10px distance from the right wall, which allows the Slider area to be exposed as a gray strip that is used to drag the Pane, again the 10px is arbitrary but it has to match the Minimum of the Slider.

How can i align the Text as Center in TextBlock in WinRT?

I have set the VerticalContentAlignment, HorizontalContentAlignment and TextAlignment as Center as per the below code but the text is not aligned center in the TextBox.
<TextBox Text="Data" Width="200" Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" TextAlignment="Center"/>
How can i achieve this without padding?
The question title says TextBlock but the code show a TextBox, so try swapping the TextBox for a TextBlock, then set the TextAlignment property to "Center" (see below):
<TextBlock TextAlignment="Center">Data</TextBlock>
Hope that helps you.

Windows phone 8 viewport control

I try to understand how windows phone viewport control Bounds work.
So i can give
viewport.Bunds = new Rect(x,y,width, height);
but what that bound stand for is it a scrollable area in the viewport.
Can anyone give me a working simple example of it cause whenever i try to use this parameter i can't scroll in viewport whatsover
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">
<ViewportControl Name="tuzik" Bounds="0,0,300,400" Margin="66,117,20,41" >
<Canvas Name="canvas">
<Image Name="TestImage" Source="Assets\testimage.jpg"
HorizontalAlignment="Left" VerticalAlignment="Top"
Canvas.Left="-379" Canvas.Top="-769" Stretch="Fill" />
</Canvas>
</ViewportControl>
<Rectangle x:Name="rect" Width="300" Height="400" Margin="60,111,63,0" Stroke="Aqua" />
</Grid>
I believe your problem is the Canvas within the ViewportControl. Canvas does not expand to fill the ViewportControl and will not expand to contain the contents, either. You have to set a Width and Height on the Canvas.
(At least, that's how I have mine setup.)

XAML, giving full width and height to my text

I have a grid with 3 rows, in my top row (200px height) i want a text on a blue background. How do i specify that my textblock should fill the space? when i add it it only fits the space the text occupy. Tried with a ViewBox, but that resized the text also, i want that to be a fixed size...
anyone?
This code is an example of size autofill for 3 different controls in given grid in XAML. Autofill is default, so you don't need to set anything additional, just make sure margin is not set. I tested this solution in MS Expression Blend and it works OK.
<Grid Height="400" Width="250" Canvas.Left="100" Canvas.Top="20">
<Grid.RowDefinitions>
<RowDefinition Height="200*"/>
<RowDefinition Height="100*"/>
<RowDefinition Height="100*"/>
</Grid.RowDefinitions>
<Button Content="Button"/>
<CheckBox Content="CheckBox" Grid.Row="2"/>
<TextBlock Grid.Row="1" Text="Test Text block" TextWrapping="Wrap"/>
</Grid>
Just remove these four properties from the xaml if they are set when TextBlock is defined, or set them to the default values:
HorizontalAlignment (Defaults to Stretch)
VerticalAlignment (Defaults to Stretch)
Width (Default to Auto)
Height (Default to Auto)
Your TextBlock should now auto stretch and fit the row.
Also make sure that your Grid is also wide enough or is set to fit your Window. Your Grid's width might also be the culprit.

Scrolling Text Block/Area/Div in Loose XAML

Is there any way to display scrollabletext in loose xaml? The equivalent in HTML would be
<div style="overflow:scroll">some long bit of text here</div>
Can you do this in loose xaml?
From my experiments so far it seems that in loose xaml:
You cannot use TextBox -- it must be TextBlock.
TextBlock doesn't seem to have any styling settings which would make it scrollable.
ScrollViewer doesn't seem to be allowed in loose xaml.
Any help gratefully appreciated.
You can use a textbox for scrolling text e.g.:
<TextBox Text="{Binding YourText}" VerticalContentAlignment="Top"
TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"
MaxHeight="200" MaxWidth="300"/>
This will show scrollbars if your text doesn't fit in the displayed area.
<ScrollViewer Height="239" VerticalScrollBarVisibility="Auto">
<toolkit:PhoneTextBox x:Name="newcTextBox" AcceptsReturn="True" TextWrapping="Wrap"/>
</ScrollViewer>