How do you completely remove the button border in wpf? - 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>

Related

Creating button with specific geometry XAML

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?

Make ErrorTemplate in Style on ComboBox allow tooltip but not mouse click

I use telerik, but that should not mean much for this question. My application is WPF (.Net 4.5).
I have a style, that I use for all comboboxes, which has an errortemplate. The style looks like this:
<Style TargetType="{x:Type telerik:RadComboBox}" x:Key="RadComboBoxStyle" >
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Background" Value="{StaticResource InputBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource InputBorderBrush}" />
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource RadComboBoxValidationErrorTemplate}" />
</Style>
My ErrorTemplate looks like this:
<ControlTemplate TargetType="{x:Type Control}" x:Key="RadComboBoxValidationErrorTemplate">
<Grid ToolTipService.IsEnabled="True" ToolTipService.ShowOnDisabled="True"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors), Converter={StaticResource ValidationErrorsConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border BorderBrush="{StaticResource ErrorBrush}" BorderThickness="3" Panel.ZIndex="999" HorizontalAlignment="Right" Margin="0,0,10,0"
Background="Transparent" DockPanel.Dock="right" Width="16" Height="16" CornerRadius="10">
<Rectangle Fill="{StaticResource ErrorBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="315" />
</Rectangle.RenderTransform>
</Rectangle>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="{StaticResource ErrorBrush}" BorderThickness="1" />
</AdornedElementPlaceholder>
</Grid>
</ControlTemplate>
The entire thing is defined in a global ResourceDictionary.
What this code does, it to put a "forbidden sign" on top of the combobox (Panel.ZIndex="999"), just before the dropdown button (using margins). The Border and the Rectangle makes a sign much like this: Picture.
The combobox itself must be able to hold a tooltip, set locally. So the error-message cannot be shown in the tooltip of the combobox - unless I find a way to combine the two without having to resolve it locally (I want that code in my resourcedictionary).
I also do not want the "forbidden sign" to handle mouse clicks (it gobbles up the click and prevent the combobox from dropping down, if the user clicks on the "forbidden sign".
I tried setting IsHitTestVisible to false on the grid and on the border inside the ErrorTemplate, but that caused the Tooltip to never show.
I also tried setting IsEnabled to false on the same two constrols, but that would not send the mouseclick on to the combobox itself (the list in the combobox does not drop down).
Is there any way to do this directly in the combobox-style or errortemplate? I do not even mind having a code behind - but I really do not want to add code locally where the combobox-style is used.

Background color for MenuItem not changed on MouseOver

I have a Menu with three items and I'm trying to change the background color when the mouse hovers over any of the items. I've tried the IsMouseOver & IsHighlighted trigger property and neither works.
In my App.xaml:
<Style TargetType="MenuItem" x:Key="MenuItemStyle" >
<Style.Triggers>
<Trigger Property="MenuItem.IsHighlighted" Value="true">
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
In my Main.xaml:
<Menu HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="975
" FontFamily="Tempus Sans ITC" FontSize="16" >
<Menu.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFC8C8C8" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Menu.Background>
<Menu.Foreground>
<SolidColorBrush Color="#FFFFFFFB"/>
</Menu.Foreground>
<MenuItem Header="New" Click="MenuNew_Click" VerticalAlignment="Center" Padding="15,4,8,3" Width="60">
<MenuItem.ToolTip>
<ToolTip>
Add new Park
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
<MenuItem Header="Search" Width="65" Padding="12,4,8,3" >
<MenuItem.ToolTip>
<ToolTip> Select search option</ToolTip>
</MenuItem.ToolTip>
<MenuItem Header="Name" Background="Black" FontSize="14" Style="{StaticResource MenuItemStyle}" />
<MenuItem Header="ID" Background="Black" FontSize="14"/>
<MenuItem Header="OwnerName" Background="Black" FontSize="14"/>
</MenuItem>
</Menu>
The default ControlTemplate for MenuItem (as extracted by Show Me The Template) doesn't set the MenuItem.Background property on mouse over, it sets an element in the template directly. Unfortunately, this means that you won't be able to just change the highlight color, but you'll have to recreate the entire ControlTemplate. MSDN has an example of how to do this (this one is from .NET 3.5 but should work for 4.0 or 4.5).
One other caveat in your code: since you're setting the Background directly on the MenuItems, your Style's Trigger would not work anyway. Because of DependencyProperty value precedence, the local value you set on the items can not be overridden by a Style Trigger.

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}.../>

Silverlight: Changing a property of a static resource at runtime

changing a static resource during runtine is something that sounds not possible.
I have a TextBox which displays a simple number. Then I have defined a style, which changes the Template of the TextBox to become a round TextBox:
<Style x:Key="RoundNumberDisplay" TargetType="TextBox">
<Setter Property="Width" Value="22"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="brd1" Width="20" Height="20" CornerRadius="15">
<TextBlock x:Name="txt1" Foreground="#222" TextAlignment="center" Text="1" FontSize="14" FontWeight="ExtraBold" VerticalAlignment="center" />
<Border.Background>
<RadialGradientBrush GradientOrigin=".3, .3">
<GradientStop Color="{StaticResource ColorBackground1}" Offset=".15"/>
<GradientStop Color="{StaticResource ColorForeground1}" Offset="1"/>
</RadialGradientBrush>
</Border.Background>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
As you can see, the displayed text is "hard-wired" in the TextBlock "txt1". So obviously, I can't change the number during runtime.
My question now is: What is the best way to change the displayed number? Creating a Style for each number looks a bit ineffective to me.
Thanks in advance,
Frank
TemplateBinding to be able to set the value of the txt1-Text-Property from the target TextBox. Important: The target-type for the ControlTemplate must be set!
<Style ...
<ControlTemplate **TargetType="TextBox"**>
...
<TextBlock x:Name="txt1" Foreground="#222" TextAlignment="center" **Text="{TemplateBinding Text}"** FontSize="14" FontWeight="ExtraBold" VerticalAlignment="center" />
</ControlTemplate>
</Style>
The Style is just the look of the control, so in practice you'll need to re-use this style multiple times. A purist might say you shouldn't include data like the numbers (which must mean something in your application context) in a Style. So you could vary the number displayed when you use the style:
<TextBox Style={StaticResource RoundNumberDisplay} x:Name="TextBoxOne" Text="1"/>
Even then, you might prefer to bind Text to your ViewModel (or whatever you use for data) and pull the number from there. Either one is Ok imo.

Resources