WPF Border partially not showing when I set a margin - wpf

I'm trying to make a WPF application and was trying to make a square border. I want it to be 10 pixels away from the top, bottom and right edges of the grid. I tried messing with the XAML code but that just produced all sorts of malformations, so I used Visual Studio's Properties tab instead. If I set the right and bottom border to 0 it looks fine, but it covers the edge like this. Code:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" Height="180" Margin="604,10,0,0" VerticalAlignment="Bottom" Width="180"/>
If I however add a 10 border to right and bottom (which is what I've used in my previous WPF apps so far) this happens. Code:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" Height="180" Margin="604,10,10,10" VerticalAlignment="Bottom" Width="180"/>
They both looks the same in the preview btw. Also sorry, I can't post direct images yet, since I don't have enough reputation.

The reason why the left border is not visible is because of the 604 as the left margin set that to 0 and you should be all good.
<Border BorderBrush="Black" BorderThickness="1" Height="180" Width="180" Margin="0,10,10,10" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
That being said, I do not recommend using fixed Height and Width in your XAML UI.
I've already got a few answers on this so feel free to check them out:
Adaptive UI with XAML
Checkout the Good practices section of this answer

Related

WPF ScollViewer cannot stretch horizontally

I got a problem with scollviewer control in WPF and searched the website, but cannot get anything.
My problem is that I have a grip inside of a scollviewer and I want to automatically stretch the gip horizontally. The code I used to test this function is as follow
<ScrollViewer HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto">
<Rectangle Fill="Red" HorizontalAlignment="Stretch" Height="100"></Rectangle>
</ScrollViewer>
I searched the problem and got the answer to use
HorizontalScrollBarVisibility="Disabled"
However, it did not work in my case.
Can anyone give suggestions. Thanks.

Make ScrollViewer scroll bound to the right

I am building a calculator application and I have a label to show the term and the result, but when the term gets too long it woudld dissappear. Because of that I implemented a scrollviewer control.
Now what I want to have is that the auto scroll of the scrollviewer is bound to the right, since my label has the content going right to left. See pictures for better explanation
What it is right now
What I want to have
This is my WPF Code for the relevant parts:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" Margin="10, 10, 10, 0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" Height="50" >
<Label x:Name="CalculatorTextBlockSolution" FontFamily="Cambria Math" HorizontalContentAlignment="Right" FontSize="24"/>
</ScrollViewer>
Add to the ScrollViewer:
FlowDirection="RightToLeft"
Update (see comments)
Add also to the Label:
FlowDirection="LeftToRight"

WPF : How to animate element width whenever it changes

This should be easy but I'm blanking out here.
I have a TextBlock inside of a Border control. The Width of the Border changes based on the length of the text in the TextBlock (updated in ViewModel). I would like this change in Width to be animated so that the width grows slowly (instead of jerky).
My Xaml:
<Border x:Name="myBorder" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock x:Name="myTextBlock" HorizontalAlignment="Left" Text="Some Text"/>
</Border>
This is a perfect job for Expression Blend. You will need to create a Storyboard animation. Then once its created go into the code behind in visual studios and play it. You can tell it to play to a certain point based on how much text you have in your text block.
Here is a basic tutorial
http://www.silverlightbuzz.com/2009/10/12/animating-with-storyboards-in-blend/
This example has back end code also
http://dotnet.dzone.com/articles/wpf-storyboard-%E2%80%93-what-it-and

WPF zoom canvas and maintain scroll position

I have a Canvas element, contained within a ScrollViewer, which I'm zooming using ScaleTransform. However, I want to be able to keep the scroll position of the viewer focused on the same part of the canvas after the zoom operation has finished. Currently when I zoom the canvas the scroll position of the viewer stays where it was and the place the user was viewing is lost.
I'm still learning WPF, and I've been going backwards and forwards a bit on this, but I can't figure out a nice XAML based way to accomplish what I want. Any help in this matter would be greatly appreciated and would aid me in my learning process.
Here is the kind of code I'm using...
<Grid>
<ScrollViewer Name="TrackScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas Width="2560" Height="2560" Name="TrackCanvas">
<Canvas.LayoutTransform>
<ScaleTransform ScaleX="{Binding ElementName=ZoomSlider, Path=Value}"
ScaleY="{Binding ElementName=ZoomSlider, Path=Value}"/>
</Canvas.LayoutTransform>
<!-- Some complex geometry describing a motor racing circuit -->
</Canvas>
</ScrollViewer>
<StackPanel Orientation="Horizontal" Margin="8" VerticalAlignment="Top" HorizontalAlignment="Left">
<Slider Name="ZoomSlider" Width="80" Minimum="0.1" Maximum="10" Value="1"/>
<TextBlock Margin="4,0,0,0" VerticalAlignment="Center" Text="{Binding ElementName=ZoomSlider, Path=Value, StringFormat=F1}"/>
</StackPanel>
</Grid>
This is not a purely XAML way of doing it, but there is a very nice piece of work on Joeyw's blog titled Pan and Zoom (DeepZoom style) in WPF with links to the source. He has taken some inspiration from DeepZoom and it gives you smooth/animated panning and zooming of content. And if you're using WPF 4 you could probably modify it a little to add some easing functions to the animations to give it an even nicer feel.

Dynamic size canvas with Scroll bars

I am developing a simple WPF application without any auto layout. The goal is when a user clicks (mouse down) a element (say textBlock) will appear at the location of the mouse click. For this I am using canvas panel embedded in a Grid of 1 row, 1 column and scrollviewer (visible). The issues are:
1. when the application window is resized the scroll viewers do not become active.
2. I want the ability to auto grow the canvas with mouse drag. Something like in MS-Excel when user drags the mouse horizontally/vertically the canvas should grow.
I have searched net a lot to figure this out and am unable to get an answer. Any help in this regard would be great.
Thanks a bunch in advance.
-P
I after asking this question I figured it out how to have freeform layout and autosize. Here is a sample XAML if anyone needs it or has better suggestion to improve this:
<Ellipse Grid.Column="0" Fill="Red"/>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch"/>
<!-- Creating a grid with one row and one column"-->
<ScrollViewer x:Name="ServerLiistCanvasScrollViewer"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
Height="Auto" Width="Auto"
Grid.Column="2" >
<Grid x:Name="drawingGrid" Grid.Column="2"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Background="Pink"
MouseDown="handleCanvasMouseDown">
</Grid>
</ScrollViewer>
</Grid>

Resources