Get rid of button border in WPF? - wpf

i am trying to get rid of button border and only display text, however a thin line around the text gets displayed even though i set borderThickness to 0 and borderbrush to transparent.
my xaml code for save 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"/>
Is there anyway i can get rid of the button border?

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>

The method that I recently found to be most useful for this was to have your button use the style of a toolbars. This will only use the image or text while only showing button borders on mouse over.
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
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" />

You need to create a new Template for your buttons.
The easiest way to do this is open your project in Expression Blend, select the button and then right click and select "Edit Template > Edit a Copy..". This copies the existing template into one you can modify. It's easier if you create it in a resource dictionary.
Then select the template and on the Resource tab (on the right of the UI) select the ButtonFocusVisual. Select the Properties tab and expand the Miscellaneous section. This has BorderStyle and BorderThickness fields (among others). Set the style to None.

Templates will not solve this problem, your only course of action is to modify the WPF control. The solution is here:
How to remove ButtonChrome border (when defining the template of a border)?

Related

WPF how to set stackpanel as resources and reuse it in TabControls

I am new to C# and WPF so please give me some ideas:
I have an WPF app used to display some stack panels,all stack panels default Visibility is set to collapsed and they will switch to visible according to the received data.
Now I want to make all these stack panels to resources so I can reuse it in some new added tab controls and stack panels.
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
</Button.Content>
</Button>
</StackPanel>
Above is one example of stack panels I am using. In the code behind the function "Color_Click" will change this "ColorOption" stack panel state and do something.
However after I try to put this stack panel into Windows.Resources
<Window.Resources>
<StackPanel x:Name="ColorOption" Visibility="Collapsed" x:Key="ColorOption">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
</Button.Content>
</Button>
</StackPanel>
</Window.Resources> (I also put the style files inside)
In the tab controls I did
<TabControl>
<TabItem Header="Tab 1" Content="{StaticResource ColorOption}"/>
</TabControl>
The visual studio shows error in the code behind says "ColorOption does not exist in the current context"
How can I fix this? Is any way to set the context? thank you
You can simply wrap the StackPanel in ContentControl and make it ControlTemplate.
<ControlTemplate x:Key="ColorOptionTemplate" TargetType="{x:Type ContentControl}">
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
</Button.Content>
</Button>
</StackPanel>
</ControlTemplate>
However, you will need to change properties of controls inside the ContentControl and it would be cumbersome. So the StackPanel could be wrapped in UserControl instead.
<UserControl x:Class="WpfApp1.ColorOptionControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
</Button.Content>
</Button>
</StackPanel>
</UserControl>
This is a common way in WPF. The downside is that you will need to add dependency properties to UserControl and wire up them with dependency properties of internal controls so that you can set their values at the level of UserControl and bridge them with external controls and window. This could be complicated and cumbersome as well.
So I think ideally it would be better to find an existing control which has similar functionalities you want and create a custom control deriving from the existing one.

WPF Button from Controltemplate isn't visible

I want to instantiate two Controls (an Expander and a Button) from two ControlTemplates. They are docked in a Dockpanel (right and left). The Expander is visible and it works fine, but the Button isn't visible.
The ControlTemplate of the Button in App.xaml:
...
<ControlTemplate x:Key="OpenFileButton" TargetType="{x:Type Button}">
<Image Name="OpenFileButton" Source="F:\AudioNodeGUI_XAML\images\filebutton.jpg">
</Image>
</ControlTemplate>
...
And the instantiation in a usercontrol:
<Grid>
<Image Source="F:\AudioNodeGUI_XAML\images\FileInputNode.jpg"/>
<DockPanel Name="dock" Width="151" Height="20" Margin="27,53,122,139">
<Expander Name="expander" Template="{StaticResource FileExpander}" Height="20" Width="41" PreviewMouseLeftButtonUp="expand" DockPanel.Dock="Left">
<ListView Name="usedFiles" Background="Black" BorderBrush="Transparent" BorderThickness="0" Width="140" Height="120" Opacity="0.5">
</ListView>
</Expander>
<Button Name="OpenFileButton" Template="{StaticResource OpenFileButton}" DockPanel.Dock="Right" />
</DockPanel>
</Grid>
But the Button isn't visible, neither in the Designer (just the outline) nor in execution. What am I doing wrong?
I suspect that your issue is just incorrect path to the image file. Unfortunately you won't have any tip about this kind of error neither in designer nor in output window. However if you have ReSharper it should highlight the path to the file if it doesn't exist

How do I make a small image button with WPF

I'm trying to create a small (21 x 21) button that has an image on it. I want all of the normal behavior/effects of a normal button, except the image should take up most of the button's face.
I created the button:
<Button Grid.Column="2" Height="21" Width="21" HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="{StaticResource CloseButton}"
Height="21" Width="21"
HorizontalAlignment="Center" VerticalAlignment="Center">
</Image>
</Button>
The image ends up squished, it looks like because of the default margin of the button?
I tried this answer which renders correctly, but then I lose the nice mouse-over effects and "click" look of the button.
The image itself is 21x21.
How can I get the effect I'm after?
Try This way, I am having the image and mouse over property in it.
<Button Width="25" Height="25">
<Image Width="21" Height="21" HorizontalAlignment="Center">
<Image.Source>
<BitmapImage UriSource="../images/icon.png"/>
</Image.Source>
</Image>
</Button>
In answer you attached target type of ControlTemplate is missing. It should be:
<ControlTemplate TargetType="{x:Type Button}">
...
</ControlTemplate>
It says your control template to have behavior of control you specify (in this case - Button)
Hope, it helps.

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.

WPF Rectangle does not have a Click event

It seems that the WPF Rectangle shape does not have the Click event defined. What am I supposed to use instead?
It does have MouseUp, but it's not quite the same behavior.
If you're not happy with MouseDown and MouseUp, perhaps you could just put the Rectangle in a Button and handle the Button's Click event?
<Button>
<Button.Template>
<ControlTemplate>
<Rectangle .../>
</ControlTemplate>
</Button.Template>
</Button>
It really depends with the behavior you're after. Please elaborate if needs be.
To add click handing to a Rectangle itself, you can use the InputBindings property:
<Rectangle Fill="Blue" Stroke="Black">
<Rectangle.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
</Rectangle.InputBindings>
</Rectangle>
This will cause the FooCommand to be executed when the rectangle is clicked. Handy if you're using MVVM!
I was looking for the related DoubleClick event and came across this suggestion to simply embed the object of interest in a ContentControl.
Here is their example (with a border, which also did not support click/double click).
<ContentControl MouseDoubleClick="OnDoubleClick">
<Border Margin="10" BorderBrush="Black" BorderThickness="2">
<Grid Margin="4">
<Rectangle Fill="Red" />
<TextBlock Text="Hello" FontSize="15" />
</Grid>
</Border>
</ContentControl>
Rectangle has event Tapped, which works fine. In designing universal apps for Windows 8.1, there are new events. Tapped, DoubleTaped, RightTapped and Holding.

Resources