Create Circular Image Xaml - xaml

In windows phone 8 I want to put an Image in a circle. Is there a container like grid which have a circular form? I know that there is ellipse bit it is not a container

Here is how I do it.
<Ellipse Width="100"
Height="100">
<Ellipse.Fill>
<ImageBrush>
<ImageBrush.ImageSource>
<BitmapImage UriSource="/YourImage.png" />
</ImageBrush.ImageSource>
</ImageBrush>
</Ellipse.Fill>
</Ellipse>
As a best practice, consider setting DecodePixelWidth and DecodePixelHeight to the same size as your ellipse.

Another option to mleroy's answer (since if I remember right WP is based on silverlight and I often run into a lack of brush availability to do stuff like that.) You could do this using the Clip property.
For example;
<Image
Source="blah\yourpicture.jpg"
Width="100" Height="100">
<Image.Clip>
<EllipseGeometry
RadiusX="100"
RadiusY="100"
Center="100,100"/>
</Image.Clip>
</Image>
Hope this helps, cheers
Edit Addition: You could also bind your radius X/Y to the width/height of the image for more flexibility on dynamic sized images.

Related

UWP border rendering issue

Either I'm going crazy or borders with a corner radius do not render correctly in UWP. Take this extremely simple example:
<Border CornerRadius="6" BorderBrush="Blue" BorderThickness="1">
<Rectangle Fill="Black" />
</Border>
And see how the rectangle extends slightly beyond border:
This happens in the designer in the visual studio and when the app is running.
It only seems to happen when there is a borderthickness >0 on the border.
Any idea why this happens?
If you are looking to apply corner radius to rectangle. Its better to use RadiusX and RadiusY properties of rectangle. Using like this will not cause any issues in rendering.
<Rectangle Width="100" RadiusX="10" RadiusY="10" Height="60" Stroke="Red" StrokeThickness="2" Fill="Black" > </Rectangle>
I managed to find a rather annoying solution by just using two borders. One with the correct border radius and style, and a second border within in for fine tuning the corners that kept clipping through.
I noticed the issues were mostly in the upper left and right corners and it was fixed by altering the corner radius of the inner border and padding of the outer border.
Source: https://stackoverflow.com/a/61468325

How can I convert a png to xaml DrawingBrush?

I have some png files that I need to use in a WPF (xaml) application in vectorial format as DrawingBrush.
How can I convert a png to xaml DrawingBrush?
A PNG is not a vector graphic, and making it one isn't something you can do by setting an attribute. You can scale it smaller, but if you try to scale it larger, you'll see some artifact. And you don't need to put it in a DrawingBrush to scale it either way.
The answer is to do this with an ImageBrush:
<Window.Resources>
<ImageBrush
x:Key="MyBrush"
ImageSource="SantaClaus.png"
TileMode="Tile"
Viewport="0,0,100,100"
ViewportUnits="Absolute"
Stretch="Fill"
/>
</Window.Resources>
<Grid Background="{StaticResource MyBrush}">
</Grid>
But if you're dealing with a poorly designed third party control which can only use DrawingBrush, you can do that too:
<DrawingBrush
x:Key="MyBrush"
TileMode="Tile"
Viewport="0,0,100,100"
ViewportUnits="Absolute"
Stretch="Fill"
>
<DrawingBrush.Drawing>
<DrawingGroup>
<ImageDrawing ImageSource="SantaClaus.png" Rect="0,0,100,100" />
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>

Creating a Hub like a Pivot on WP 8.1

What I'm trying to do was extremely easy in SL/WP 8 but seems to be impossible in WP 8.1 without redefining the Hub template myself. I want to create a hub with a header that:
Scrolls horizontally.
Has a background that scrolls along with it.
Has no margins on either side.
I know this can probably be solved by just having my background image include the background and the hub just being transparent, but I wanted to know if there was a way to solve it in XAML.
Putting a Grid with a background into the Hub's header just highlights the background as much as the hub needs--not stretching all the way across:
<Hub>
<Hub.Header>
<Grid Background="Red" Height="60">
<TextBlock Text="My Header" />
</Grid>
</Hub.Header>
</Hub>
The above makes the header with the text "My Header" but only the text part has a background. Furthermore, the Hub itself seems to have inner margins of 16 on each side so the background doesn't stretch across the whole phone screen.
Should I just be going with a background or deconstructing the template to remove the margins?
Far from an elegant solution but basically I put the background outside the Hub and gave it negative margins like so. Hacky but I guess it works.
<Grid>
<!-- This is the header bar -->
<Grid Height="64" Background="Red" />
<Hub>
<Hub.Header>
<StackPanel Margin="-6,0,0,0">
...
</StackPanel>
</Hub.Header>
<!-- actually just defined the margin in my ResourceDictionary to target all HubSections -->
<HubSection Header="section 1" Margin="-2,-20,-4,8" />
<HubSection Header="section 2" Margin="-2,-20,-4,8" />
</Hub>
</Grid>

Slider overflow

I have popup-like border in my page. There is slider inside the popup. The slider has range from 0 to 100, but when I slide it to the right edge I get somewhere near to vlaue 93. Slider is full but its maximum is 100. It seems that slider overflowed the parent container. I tried to use all combinations of margins and static widths, but without success. Can anyone tell me what I am supposed to set, to get it work?
Here is fragment of code:
<Grid x:Name="LayoutRoot" >
...
<Border VerticalAlignment="Center" Margin="24,0" Visibility="{Binding ...}">
<StackPanel>
<TextBlock Text="choose desired position" />
<Slider x:Name="sldGoto" Maximum="100" SmallChange="1" LargeChange="10" Value="93"/>
</StackPanel>
</Border>
</Grid>
With this code (value of slider set to 93) is slider full. What's wrong?
This is a known bug in the current release when using Slider on Windows Phone 7 with the standard control template. I recommend using the approach you found on Dave's blog for now.
Well I just find article with nice Slider ControlTemplate that just works.
I would still appreciate if anyone could confirm this behavior or told me what I did wrong.
Thanks

Is there any way to do a Drop Shadow effect in XAML on a TextBlock?

I have a XAML TextBlock that I would like to render as a drop shadow. That is a white layer of text on top of a black layer. I'm doing this to make the text stand. Currently I have two TextBlocks offset to the right and below by two pixels. The top layer is white and the bottom layer is black.
Is there a simple way to do this in XAML? If so can you please provide an example?
Bitmap effects are deprecated. Use the new GPU-accelerated DropShadowEffect instead.
<TextBlock>
<TextBlock.Effect>
<DropShadowEffect />
</TextBlock.Effect>
Drop.
</TextBlock>
<TextBlock
Text="Shadow Text"
Foreground="Teal">
<TextBlock.BitmapEffect>
<DropShadowBitmapEffect
ShadowDepth="4"
Direction="330"
Color="Black"
Opacity="0.5"
Softness="0.25" />
</TextBlock.BitmapEffect>
</TextBlock>
There are plenty of other examples on MSDN