Show different background for transparent buttons - wpf

We have a design requirement such that transparent buttons (with white text) are overlayed on a scenic background image. But here's the hard part, and my dilemma...
While the background image should be clear and visible OUTSIDE the image borders, the area INSIDE the buttons (the overlayed area) should be a blurred area of the background image. The idea is to make it easier for users to see the white text inside the button.
I'm a novice in XAML and even more unfamiliar with how to perform such complex styling. I have a starting point for the overlay layout, but this code does not attempt to solve the problem. It's simply a button over an image.
Any ideas or help on how to blur the area under the image?
<Grid Margin="0,0,0,12" Visibility="{Binding SignedIn, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=False}">
<Image Source="backgroundImage.png" Height="136" Stretch="Fill" HorizontalAlignment="Stretch" />
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Uid="SignInBtn" Content="SIGN IN" Style="{StaticResource TransparentButtonStyle}" HorizontalAlignment="Center" Command="{Binding SignInCommand}" IsEnabled="{Binding LoadingResults, Converter={StaticResource NotBoolConverter}}" />
</StackPanel>
</Grid>

There's no straightforward way to do this in a Windows Phone 8.1 app. Image effects are not supported in either Windows Phone Silverlight or in Windows.UI.Xaml.
The typical and easy solution would be to dim (rather than blur) the image by setting the Button's background to a partially transparent brush.
<Button Background="#7F000000" </Button>
If you want to put a bit more work into it you can crop the area of the bitmap behind the button, run your blur transform on it (take a look at the Lumia Imaging SDK) and then set the new, blurred image to the button.

Consider using Effects.
In addition, see if you can leverage this example:
<Style TargetType="Window">
<Setter Property="Background">
<Setter.Value>
<VisualBrush>
<VisualBrush.Visual>
<Image Source="Images\myImage.png">
<Image.Effect>
<BlurEffect Radius="20"/>
</Image.Effect>
</Image>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
...

Related

WPF Hyperlinked Image with rounded corners

Question: How to create hyperlinked Image with rounded corners in WPF/XAML?
So far, existing code for hyperlinked image (no rounded corners) is working (see below):
Hyperlinked Image (WPF XAML)
<TextBlock Name="txtbFooterRight" >
<Hyperlink Name="lnkImg" TextDecorations="None"
NavigateUri="http://webinfocentral.com"
ToolTip="Navigate to web page">
<Image Name="someName" Source="some url" />
</Hyperlink>
</TextBlock>
Hyperlinked image code behind (C#):
lnkImg.RequestNavigate += (s, e) => {Process.Start(e.Uri.ToString()); };
Image control with rounded corners (no hyperlinks) is implemented as:
Image with rounded corners (WPF/XAML):
<Border Name="brdRounded" BorderThickness="0" CornerRadius="10">
<Border.Background >
<ImageBrush>
<ImageBrush.ImageSource>
<BitmapImage UriSource="some Uri to .jpg" />
</ImageBrush.ImageSource>
</ImageBrush>
</Border.Background>
</Border>
I need to "round the corners" of the hyperlinked image (WPF/XAML), probably combining aforementioned techniques. Thanks and regards,
Note: I've accepted the answer posted by user #lisp with just minor fix: Border background color should match the surrounding color in order to avoid slight "color leakage". Kudos to the author!
On a separate note: it was an eye-opening experience on how relatively difficult it is to achieve such simple effect while using WPF/XAML comparing to HTML5/CSS3 (see for, example, essentially the same effect on rounded corner image at: http://infosoft.biz/SlideShowCSS.aspx). It seems like WPF folks at Microsoft should take a note...
Border is used for rounded corners. But in your case if you simply put TextBlock inside of Border, you wouldn't get the desired effect.
Here Corners are made transparent using a border. Grid is used so that Border stretches exactly to the size of TextBlock.
<Grid>
<Border Name="CornersMask" Background="White" CornerRadius="20"/>
<TextBlock>
<TextBlock.OpacityMask>
<VisualBrush Visual="{Binding ElementName=CornersMask}"/>
</TextBlock.OpacityMask>
<Hyperlink ...>
<Image Name="someName" Source="some url" />
</Hyperlink>
</TextBlock>
</Grid>
TextBlock is displayed on top of Border, and because of that and antialiasing you may experience slight whiteness on the rounded edges. Either change the Border background to the surrounding background color, or enclose Border in another container that will autostretch it e.g. Border of Grid, and set it's Visibility to Hidden.
<Border Visibility="Hidden">
<Border Name="CornersMask" Background="White" CornerRadius="20"/>
</Border>
This also solves the problem when the surrounding background is not a SolidColorBrush

TextBox with background image and color

I have a TextBox control and I would like to be able to set a background image and a background color.
Currently I can set one, or the other but not both. When I try to set both simultaneously I receive a "The property 'Background' is set more than once" error.
Here is the code I used:
<TextBox Name="tbImageTextBox">
<TextBox.Background>
<ImageBrush ImageSource="/Resources/Images/image.png"
AlignmentX="Right" Stretch="None"/>
<SolidColorBrush>#FF8D8A8A</SolidColorBrush>
</TextBox.Background>
</TextBox>
I have also attempted to set the background color in the style for the TextBox and the image in the <TextBox.Background>, but the color is ignored.
Use the grid resource for background as needed. Same resource can be used for multiple textboxes.
<Grid>
<Grid.Resources>
<ImageBrush x:Key="img" ImageSource="Blue hills.jpg"></ImageBrush>
<SolidColorBrush x:Key="brownBrush" Color="Brown"></SolidColorBrush>
</Grid.Resources>
<TextBox x:Name="test" Background="{StaticResource img}" Width="100" Height="40" />
</Grid>
I ended up putting the TextBox into a grid with the Background color set and applying the background image to the TextBox itself as using VisualBrush and DrawingBrush stretched my image or only applied the background color to the image - not the rest of the TextBox.
You will need to combine the colour and the image in a single Brush instance, you could use a DrawingBrush or a VisualBrush containing an Image control with your image and the Background set to the colour for example.
You probably want either a VisualBrush or a DrawingBrush. More information on those can be found at MSDN here. Something like this might get you started:
<Rectangle Width="75" Height="75">
<Rectangle.Fill>
<VisualBrush TileMode="Tile">
<VisualBrush.Visual>
<Grid>
<Image BaseUri="somepic.png" />
<Rectangle Brush="FF8D8A8A" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>

wpf button with foreground and background as image

In my wpf application I have a button. I want the button like to be background have an image and forground also have the image. How to do this?
Thanks in advance,
Karn
Not sure I understand what you want here, but here's two examples
Two different Images for foreground and background
<Button Content="Button" FontSize="50" Margin="0,0,263,155">
<Button.Background>
<ImageBrush ImageSource="C:\Time.png"/>
</Button.Background>
<Button.Foreground>
<ImageBrush ImageSource="C:\C1.png"/>
</Button.Foreground>
</Button>
Same Image as Foreground and Background
<Button Content="Button"
FontSize="50"
Foreground="{Binding RelativeSource={RelativeSource self}, Path=Background}">
<Button.Background>
<ImageBrush ImageSource="C:\Time.png"/>
</Button.Background>
</Button>
In general, you need to override the button's default appearance using a Style and / or a ControlTemplate.
For examples, you may want to see:
Using Templates to Customize WPF Controls, from MSDN Magazine (great starting point!)
This StackOverflow question on creating image buttons
Feel free to provide more specifics or some code if you're having trouble, and we'll be able to help further.

WPF Image Button formatting

If I create a button with an image inside, it gets blown up to much larger size than the image.
If i try to constrain the size of image, and container button, the image gets cut off:
<Button Background="Transparent" Width="18" Height="18" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}">
<Image Width="16" Height="16" />
</Button>
Native image size is 16x16. Here the button is 18x18 with 0 padding, yet the image still gets cut-off on right/bottom.
how can i make the entire button be exactly the size of the image w/out any cut off?
The beauty (and curse?) of WPF is the ability to re-template controls.
You can set the template of the button something like the following (this is free hand, so you will need to tweak it a bit for your tastes):
<Button x:Name="btn16x16">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<Image Source="pack://siteoforigin:,,,/Resources/SixteenBySixteen.png"
Width="16"
Height="16"/>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
You should remove any Width and Height settings and set the Button's HorizontalAlignment and VerticalAlignment both to Center (or Left/Right or Top/Bottom resp.). By default, these properties are set to Stretch which makes the button stretch to the available space. Since the image is the content of the button, it also gets stretched. Something like this should work:
Take this code example:
<Button Background="Transparent" BorderBrush="{x:Null}" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="thePathToYourImage" Stretch="None"/> <!-- EDIT: added Stretch="None" here! -->
</Button>
.
The following was just meant as an alternative with a different result which obviously (considering your comments) is not what you want.
If you want the button to stretch, but not the image, you could also set the Image's alignment properties to something other than Stretch. Or you could set its Stretch property to None. This would make the button as large as possible, but keep the image at its original size (16x16). This would work as follows:
<Button Background="Transparent" BorderBrush="{x:Null}">
<Image Source="thePathToYourImage" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button>
<!-- OR: -->
<Button Background="Transparent" BorderBrush="{x:Null}">
<Image Source="thePathToYourImage" Stretch="None"/>
</Button>
You can use Button.Background and ImageBrush as an exampe like this:
<Button Height="24" Width="24" >
<Button.Background>
<ImageBrush ImageSource="image name or path" Stretch="Fill" TileMode="None" />
</Button.Background>
</Button>
Think this might give people the answer they are looking for, buttons with image and no borders at all.

Double Border with a VisualBrush in WPF

I'm curious if anyone knows of a way to easily get a double border effect in WPF similar to what you see in the selected items in Windows Explorer in Windows 7.
If you look closely, you'll notice that the selected item has a dark border, a lighter, inner-border, and then a gradient background.
Currently, I'm using two borders around an object any time I want to achieve this effect. Doing it that way is ugly syntactically and really muddies my view xaml. Being a web developer at heart I'd like to separate the xaml structure from the style as much as possible. So, I've started putting this into Styles and Content Templates in order to pull it out of my view xaml.
However, I'm curious if there may be a better way to go about this.
I played around for a while using a VisualBrush as a background to try to get the effect. However, I want to be able to apply the background to elements that can be any size and the way the VisualBrush stretched the visual to fit the element background didn't work the way I wanted it to. Essentially, I'd really just like it to stretch the visual the way the WPF layout system would do it.
Any ideas would be greatly appreciated.
--
Dusty
A VisualBrush is probably not what you want to do in this scenario, as it's pretty heavy.
You can solve the problem with some Xaml without nesting borders.
For example,
<Border BorderBrush="#FF00B5C5" BorderThickness="1" CornerRadius="2" Background="White">
<Grid Background="#FF00B5C5" Margin="1">
<Rectangle Fill="#FFA2F2FE" />
<TextBlock Text="This is some text" VerticalAlignment="Center"/>
</Grid>
</Border>
You can, of course, tweak the properties to get the look you need.
EDIT: If you want to create a style, so you can reskin the look-and-feel, you can do something like this:
<Window.Resources>
<Style x:Key="BorderedTextBlock" TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border BorderBrush="#FF00B5C5" BorderThickness="1" CornerRadius="2" Background="White">
<Grid Background="#FF00B5C5" Margin="1">
<Rectangle Fill="#FFA2F2FE" />
<TextBlock Text="{Binding}" VerticalAlignment="Center"/>
</Grid>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ContentControl Style="{StaticResource BorderedTextBlock}" Content="This is some text" Width="200" Height="24"/>
</Grid>
Additionally, turn this into a custom control with all the styling and theming parameters that you need.
Hope that helps,
Sergio

Resources