have transparent button text - wpf

I have a semi transparent grid on a window . I have placed a button in grid cell.
now I want to be able to set the button text transparent, but not background ie., I want to set some background color to button and I want to be able to see through the button text.
Please somebody could help me on this.

I am a bit confused what you mean to acquire but if you want to be able to read across text you can cut it out. To do this you create for instance a square, put a text into it and cut out this text (for instance in gimp) so you will obtain a png file which looks like first picture (at first glance foreground reminds white but it is transparent). Then you add picture to project and
<Button Opacity="1" Background="Goldenrod" Width="100" Height="100">
<Button.OpacityMask>
<ImageBrush ImageSource="fileWithText.png"/>
</Button.OpacityMask>
</Button>
The second picture depicts result. Background of grid is set to blue what we can presume thanks to text which is cut out. It is not efficient way out since not everything can be done only in WPF but I hope you will take advantage of it.

Related

WPF text scroll animation

How do you create a control that horizontally scrolls the text to show text that are to long?
So instead of character overflow i would like it to scroll from side to side.
I've seen controls that does a continues scroll but that's not what i look for.
Not clear wih your Question. Something like this.
<TextBox Width="500" Height="50" ScrollViewer.HorizontalScrollBarVisibility="Auto"/>
Dont know where I have to put animation here :)

How to mix text-only RibbonToggleButton and image-only RibbonButton in Ribbon for WPF?

I'm using the Microsoft Ribbon for WPF October 2010 and have got 3 buttons next to each other, inside a RibbonControlGroup. The left and the right ones are text-only RibbonToggleButton controls, the one in the middle is an image-only RibbonButton. The buttons have a defined width to match the row above.
My code:
<r:RibbonControlGroup>
<r:RibbonToggleButton Width="110" Label="Outgoing" IsChecked="True" />
<!-- Padding and Height set to align the buttons/image -->
<r:RibbonButton Width="30" Padding="5 0" Height="24" SmallImageSource="Images\Small\arrow_swap.png" />
<r:RibbonToggleButton Width="110" Label="Incoming" />
</r:RibbonControlGroup>
The problem is that as soon as I apply the SmallImageSource to the middle button, the other two get spacings for images, causing the text not to be centered anymore. That is problem (1).
Because I didn't find a quick solution, I tried to add the image of the middle button not via SmallImageSource, but by adding it as <Image> for the content of the button. However, the button would remain empty. That is problem (2).
My third solution was to add images to the left and the right button, too. Unfortunately there is nearly no margin between the image and text, which looks quite ugly. I tried several things to enlarge the margin like adding a <Style TargetType="Image"> to the <RibbonToggleButton.Resources>, but although the editor accepts it and displays the spacing at designtime, the margin is gone again at runtime. That is problem (3).
Does anybody have an appropiate solution to any of the three problems? I cannot get it to work. The number of the problems is also the preference of the alternatives, (1) being the most favourite to use with an appropiate hack.
It seems you cannot do that. Its by design.
As per MSDN (here about half way down the page):
Related ribbon controls can be grouped together in a RibbonControlGroup. When a control group is resized, one RibbonControlSizeDefinition is applied to all of the controls in the RibbonControlGroup. The RibbonControlGroup is positioned in the RibbonGroup as if it were one control.
All the controls in a RibbonControlGroup shares the same RibbonControlSizeDefinition. Hence, in your case image will be added for all the buttons.
You may use RibbonGroup instead if you do not want to glue all your controls very closely. This way you can customize each button container by using different RibbonControlSizeDefinitions.

How can I create an opacity mask in wpf that doesn't scale?

Ok, I've created a PNG-24 with transparency. It's basically a grayscale image that uses 'colors' in between black and transparent instead of black and white. I did this so I can use this as the Opacity Mask of a colored rectangle, thus rendering the image in whatever color I want using only a single graphic.
However, for the life of me, I can't get WPF to stop anti-aliasing the da*n image!!
I've set 'SnapesToDevicePixels' on the rectangle to which the brush is applied... I've set the ImageBrush's Scale to 'None'... I've set its ViewPort and the ViewBox to absolute units and sized them exactly to the source image. But no matter what I try, WPF still insists on trying to smooth things out! This is VERY frustrating!!!
So... anyone know how to use an image as an opacity mask but not lose the pixel-precise drawing that we have done? I just want WPF to render the damn thing as we drew it, period!
I have tried to reproduce your problem. Simply like this:
<Rectangle Width="200" Height="200" Fill="Red">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="/mask.png"/>
</Rectangle.OpacityMask>
</Rectangle>
mask.png contains a simple diagonal mask, like that half of rectange is visible and other half is 100% transparent.
And recrangle is rendering pixel perfect (and aliased, as you want).
I think, that you may a DPI setting, that is not native to your monitor, and WPF just can`t render images correctly.
GOT IT! It's a layout issue that for some reason, there's no easy way to change. However, there's a value you can set called UseLayoutRounding that fixes it. I just set it at the root level (for this fauxample, a grid...)
<Grid UseLayoutRounding="True">
....
</Grid>
...and BAM! Works like a charm! "Sort of" like a 'SnapsToDevicePixels' but for positioning of elements (i.e. it rounds all layout-related values like left, width, etc. whereas SnapsToDevicePixels snaps the layout to the on-screen pixels when rendering.)
M

How can I change the color of an image in silverlight/WP7 (Color Mask)?

I have a toggle button with a png that has a transparent background and a black foreground. If the button is selected then I want the black color of the image to change to a color chosen by the user. Is there a way to do this in Silverlight and/or wp7?
So for example:
<ToggleButton>
<Image Source="MyImage.png" />
</ToggleButton>
MyImage.png has a transparent background and a black foreground. The user's preferred color is red. When the button is toggled on I want the black foreground of the image to turn red.
I would try OpacityMask approach. Basicaly it should look something like this:
<Rectangle Fill="Red">
<Rectangle.OpacityMask>
<ImageBrush ImageSource="MyImage.png"/>
</Rectangle.OpacityMask>
</Rectangle>
by changing rectangle's fill property you would get different colored image.
How much control does the user have over the colour?
If they are picking from a limited set (e.g. red, green, blue, black, brown) then the simplest thing to do would be to bind the image source to a variable which holds the name of the image and then change which name is held in that variable.
If they can pick any colour then you need to do some image processing to change the black pixels of a reference image to the selected colour, write that to isolated storage and then bind the image source to that new file.
Another alternative is to draw the button in XAML and then you can have direct control over the foreground colour. This MSDN page describes the basics of drawing. You can use the same commands to define the image on a button, as described on this page from Scott Gu's blog:
(this is the image of the code from the blog).
If you bind the colour to a variable then the user can change the colour of the image. It does rely on you being able to draw the image in XAML though.

Absolute positioning in WPF

I have a long text and show first sentence in a TextBlock.
I wish by clicking the TextBlock or a button to show a panel below the TextBlock with full text. I wish this panel be absolutely positioned and be displayed above any other elements, you can do a similar thing in HTML showing and hiding absolutely positioned 'div' element.
How to do this in WPF?
Thank you for any suggestions.
AdornerLayer can work, but may be a little complex. Other options include using PopUps or ToolTips -- you should look into those first as your easiest options.
If these all don't work, it'll really depends on what kind of panel you're using. For example, if you're using a Canvas, all you have to do is make sure to set the correct ZIndex on the element.
In order to make this more robust, I'd suggest the following:
<!-- Set Panel.ZIndex="99" when showing hidden area to ensure top placement -->
<Grid>
<TextBlock>This is my primary bit of text ...</TextBlock>
<!-- Canvas stays hidden until we want to show the rest of the text -->
<Canvas Visibility="Hidden">
<TextBlock Canvas.Bottom="-10">Content goes here</TextBlock>
</Canvas>
</Grid>
Put the long text in an AdornerLayer is the best option. Check out some links
http://msdn.microsoft.com/en-us/library/ms743737.aspx
http://wangmo.wordpress.com/2008/10/19/relations-between-adorner-adornerlayer-and-adornerdecorator/

Resources