Creating button with specific geometry XAML - wpf

Please can anyone suggest a solution of my problem.
Most "custom buttons" on different tutorials based on standard geometry forms (triangle, rectangle, circle etc.) anŠ² changed only their appearance.
I trying to create button with specific geometry and changed it on mouse event.
what I have:
I created style where diskribe
I created a polygon in the form of a rectangle with rounded corners (this is important)
I created style where described my custom button, and in main window i added the Style attribute:
<Button Width="40" Margin="5,0,5,-15" DockPanel.Dock="Right" HorizontalAlignment="Right" Background="{x:Null}" BorderBrush="{x:Null}" Height="40" Style="{StaticResource ResourceKey=PLCButtonStyle}"/>
this is how i described geometry:
</Style.Resources>
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}" >
<Grid>
<MAIN:RoundedCornersPolygon Name="Border" Points="0,0 75,0 75,15 75,30 0,30 "Stretch="Fill"
StrokeThickness="1" ArcRoundness="5" UseRoundnessPercentage="False" Stroke="Black" IsClosed="True
<MAIN:RoundedCornersPolygon.Fill>
<RadialGradientBrush RadiusX="1" RadiusY="1" GradientOrigin="0.7,0.3" >
<GradientStop Color="lightgray" Offset="0" />
<GradientStop Color="black" Offset="1" />
</RadialGradientBrush>
</MAIN:RoundedCornersPolygon.Fill>
</SCADA:RoundedCornersPolygon>
I need to animate the transition of certain points of the polygon from one coordinate to another.
from Points="0,0 75,0 75,15 75,30 0,30 "
to Points="0,0 60,0 75,15 60,30 0,30 "
what i need to do for this?

Related

Border.Background Effect

I have been trying Microsoft.Expression.Media.Effects to create a MonoChromeEffect on UIElements.
xmlns:e="http://schemas.microsoft.com/expression/2010/effects"
<Border.Effect>
<e: MonochromeEffect Color="Blue" />
</Border.Effect>
This works fine for any control with any Color.
My problem is when attempting to apply to an ImageBrush. (adding blue mono-tint to just the background).
<Border.Background >
<ImageBrush ImageSource="{Binding WaterMarkImage}"
Stretch="UniformToFill" Opacity=".34"
RenderOptions.BitmapScalingMode="HighQuality"
AlignmentX="Center" AlignmentY="Top" />
</Border.Background>
This will colorize all controls inside the Border - (including for instance - ListBoxItems to mono-blue).
Is there anyway to just affect just the ImageBrush - (not the list items - (keep them White)?
You may use a VisualBrush with an Image control instead of an ImageBrush and apply the effect to the Image:
<Border.Background>
<VisualBrush>
<VisualBrush.Visual>
<Image ImageSource="{Binding WaterMarkImage}"
Stretch="UniformToFill" Opacity=".34" ...>
<Image.Effect>
<e:MonochromeEffect Color="Blue"/>
</Image.Effect>
</Image>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>

Creating a block of text with Silverlight

All i am trying to do create rectangles with radial corners and content within them. Content can be any image, textual data or multimedia something like http://www.spicynodes.org/. so
How do I create that rectangle ( can i have xaml markup)
Can i have arrows from one rectangle to another if so how?
the nearest I got to was below but unable to add text data
<Grid Name="containerPanel" Width="800" Height="500" Background="AntiqueWhite" VerticalAlignment="Center" HorizontalAlignment="Center">
<Rectangle Name="centerNode" Width="300" Height="150" RadiusX="12" RadiusY="12" VerticalAlignment="Center">
<Rectangle.Effect>
<DropShadowEffect ShadowDepth="3" BlurRadius="2" Color="Black"></DropShadowEffect>
</Rectangle.Effect>
<Rectangle.Stroke>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Gray" Offset="0.1"></GradientStop>
<GradientStop Color="Beige" Offset="0.2"></GradientStop>
</LinearGradientBrush>
</Rectangle.Stroke>
<Rectangle.StrokeThickness>
2
</Rectangle.StrokeThickness>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="AliceBlue" Offset="0.4" />
<GradientStop Color="White" Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
Instead of using a Rectangle, you might want to use a container control. Assuming the content of your will consist of one element (i.e.: Either an image, either a textblock, either a ....), the first thing that comes to mind would be to use a Border, which will accept exactly one child element as its content:
<Border Width="300" Height="200" BorderBrush="Green" BorderThickness="5" CornerRadius="10">
<Border.Effect>
<DropShadowEffect ShadowDepth="5"/>
</Border.Effect>
<TextBlock Text="Inside the bounding box" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
As for the arrows going from one box to another:
Of course it's possible, but you will have to "connect" them manually. There's no out-of-the-box "connect and draw an arrow between these two elements" functionality.
Why not use a container like another grid, style it as you prefer, then add an image or textblock element inside it instead of a rectangle?

Button with custom ControlTemplate sometimes misses clicks

My Button with a custom ControlTemplate does not respond to every click. I have no idea why, since it is totally random if the click works or not.
ControlTemplate:
<ControlTemplate x:Key="KreisPlus">
<Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607
17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,
32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,
43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
Height="20" Stretch="Fill" Stroke="#FFD7D7D7" Width="20">
<Path.Fill>
<RadialGradientBrush>
<GradientStop Color="#FF1F71D5" />
<GradientStop Color="White" Offset="0.94" />
<GradientStop Color="#FF6499E0" Offset="0.78" />
</RadialGradientBrush>
</Path.Fill>
</Path>
</ControlTemplate>
<Button Click="Button_Click" Template="{StaticResource ResourceKey=KreisPlus}" />
just try this code sample and see is that what you expect?
<ControlTemplate x:Key="KreisPlus" TargetType="{x:Type Button}">
<Grid HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Transparent" >
<Path Data="M74,37.5 C74,57.658393 57.658393,74 37.5,74 C17.341607,74 1,57.658393 1,37.5 C1,17.341607 17.341607,1 37.5,1 C57.658393,1 74,17.341607 74,37.5 z M32.113861,13.5 L43.386139,13.5 43.386139,32.613861 62.5,32.613861 62.5,43.886139 43.386139,43.886139 43.386139,63 32.113861,63 32.113861,43.886139 13,43.886139 13,32.613861 32.113861,32.613861 z"
Height="20"
Stretch="Fill"
Stroke="#FFD7D7D7"
Width="20">
<Path.Fill>
<RadialGradientBrush>
<GradientStop Color="#FF1F71D5" />
<GradientStop Color="White"
Offset="0.94" />
<GradientStop Color="#FF6499E0"
Offset="0.78" />
</RadialGradientBrush>
</Path.Fill>
</Path>
</Grid>
</ControlTemplate>
Now the path is inside the Grid and so it can respond to the click. This will take the entire area and only the path in the center. So if you click on the button anywhere even outside path will execute the click. If you don't want this and you want the path should respond to the click just change horizontal and vertical alignment as center
HorizontalAlignment="Center"
VerticalAlignment="Center"

Display image in content presenter in button

I have a button with a style that displays an image inside it. I would like to be able to specify the image it uses using the Content property on the button (or some other means).
How can accomplish this without actually nesting an image directly in the button.
<BitmapImage x:Key="closeImage" UriSource="close.png" />
I thought I could maybe specify the image file name like so:
<Button Content="{{StaticResource closeImage}" x:Name="closeButton" Click="closeButton_Click" Style="{DynamicResource WindowToolboxButton}"/>
Style:
<Style x:Key="WindowToolboxButton" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{StaticResource ButtonNormalBackgroundFill}"/>
<Setter Property="BorderBrush" Value="{StaticResource ButtonBorder}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="grid" Height="15" Width="15">
<Border x:Name="border" CornerRadius="2,2,2,2" BorderBrush="#FFBBCDD2" BorderThickness="1" Opacity="0" Margin="0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF82A3AC" Offset="1"/>
<GradientStop Color="#7FCDD9DC"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Image x:Name="image" Source="close.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" >
</Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'm pretty sure I'm missing the problem of your question, but the following code works for me:
<Window.Resources>
<Image x:Key="test" Source="/Images/ChangeCase.png"/>
</Window.Resources>
<!-- Using a Resource for the Content -->
<Button Width="100" Height="20" Content="{StaticResource test}"/>
<!-- Specifying the Content directly -->
<Button Width="100" Height="20">
<Image Source="/Images/ChangeCase.png"/>
</Button>
Plus I spotted an error in your code:
<Button Content="{{StaticResource closeImage}" [...]
should be:
<Button Content="{StaticResource closeImage}" [...]
I don't quite understand what your style is doing. Why do you have the Content Property set to a StaticResource when you want to specify the image via style?
Edit:
Alright, I tried your style and it also works for me.
<Style x:Key="WindowToolboxButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="grid" Height="15" Width="15">
<Border x:Name="border" CornerRadius="2,2,2,2" BorderBrush="#FFBBCDD2" BorderThickness="1" Opacity="0" Margin="0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF82A3AC" Offset="1"/>
<GradientStop Color="#7FCDD9DC"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Image x:Name="image" Source="/Images/ChangeCase.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Width="100" Height="20" Content="Test" Style="{DynamicResource WindowToolboxButton}"/>
The image is shown. However, I can't see your border due to the opacity-prop being set to 0.
The content that is given directly on the button is overriden by the style.
Maybe there is sth wrong with your image? Did you set its build-property to Resource?
I think the correct way of doing this is
Create a new custom control ImageButton which derives from Button
Create a DP 'ImageSource' of type 'ImageSource' in ImageButton and in its style bind the Source of the Image to this DP.
Then you can use it like <ImageButton ImageSource={StaticResource ...}/>
If you try to add an image defined in a resource to a content control it will only work the first time. The second time you try to add the same image into another content control it will fail with "Specified visual is already a child of another visual..."
Actually, this is really easy. Use the Style you already have and add the following to 'image'
Source="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
Then (as long as you have the image set as Content/Copy if newer) you can set the Content with the desired image's Uri.
<Button Content="/[projectname];component/.../close.png" Style="{StaticResource WindowToolboxButton}" ... />
note: replace '[projectname]' with your project's name and the '...' with path to the image
Replace
<Image x:Name="image" Source="close.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" >
With
<Image x:Name="image" Source="{Binding Content, RelativeSource={RelativeSource AncestorType=Button}" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" >
Where your ancestor type will be That button that is called in xaml, and there you can Set Content of that button to point to some image.
Example:
<cc:customButton Content={StaticResource someImage}.../>

How do you completely remove the button border in wpf?

I'm trying to create a button that has an image in it and no border - just like the Firefox toolbar buttons before you hover over them and see the full button.
I've tried setting the BorderBrush to Transparent, BorderThickness to 0, and also tried BorderBrush="{x:Null}", but you can still see the outline of the button.
Try this
<Button BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >...
You may have to change the button template, this will give you a button with no frame what so ever, but also without any press or disabled effect:
<Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And the button:
<Button Style="{StaticResource TransparentStyle}"/>
What you have to do is something like this:
<Button Name="MyFlatImageButton"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Padding="-4">
<Image Source="MyImage.png"/>
</Button>
Hope this is what you were looking for.
Edit: Sorry, forgot to mention that if you want to see the button-border when you hover over the image, all you have to do is skip the Padding="-4".
I don't know why others haven't pointed out that this question is duplicated with this one with accepted answer.
I quote here the solution: You need to override the ControlTemplate of the Button:
<Button Content="save" Name="btnSaveEditedText"
Background="Transparent"
Foreground="White"
FontFamily="Tw Cen MT Condensed"
FontSize="30"
Margin="-280,0,0,10"
Width="60"
BorderBrush="Transparent"
BorderThickness="0">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
</Button>
You can use Hyperlink instead of Button, like this:
<TextBlock>
<Hyperlink TextDecorations="{x:Null}">
<Image Width="16"
Height="16"
Margin="3"
Source="/YourProjectName;component/Images/close-small.png" />
</Hyperlink>
</TextBlock>
You may already know that putting your Button inside of a ToolBar gives you this behavior, but if you want something that will work across ALL current themes with any sort of predictability, you'll need to create a new ControlTemplate.
Prashant's solution does not work with a Button not in a toolbar when the Button has focus. It also doesn't work 100% with the default theme in XP -- you can still see faint gray borders when your container Background is white.
Why don't you set both Background & BorderBrush by same brush
<Style TargetType="{x:Type Button}" >
<Setter Property="Background" Value="{StaticResource marginBackGround}"></Setter>
<Setter Property="BorderBrush" Value="{StaticResource marginBackGround}"></Setter>
</Style>
<LinearGradientBrush x:Key="marginBackGround" EndPoint=".5,1" StartPoint="0.5,0">
<GradientStop Color="#EE82EE" Offset="0"/>
<GradientStop Color="#7B30B6" Offset="0.5"/>
<GradientStop Color="#510088" Offset="0.5"/>
<GradientStop Color="#76209B" Offset="0.9"/>
<GradientStop Color="#C750B9" Offset="1"/>
</LinearGradientBrush>

Resources