Solved

C# WPF - how to add border property to a WPF button programatically in C#

Posted on 2013-06-17
5
1,229 Views
Last Modified: 2013-06-18
I am trying to create a round WPF button programmatically.  I am not sure how to attach the border property. I understand the border property is the one give round corners.

Any sample how to do that will be appreciated.
0
Comment
Question by:tommym121
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:jonnidip
ID: 39255575
The border property should be set at the template level.
Here is a XAML sample of how to style your control to a button:
<Button Width="100" Height="30">
    <Button.Style>
        <Style>
            <Style.Setters>
                <Setter Property="Button.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border CornerRadius="10,10,10,10" BorderThickness="1,1,1,1" BorderBrush="Black"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </Button.Style>
</Button>

Open in new window


If you need to create a button from code, you may want to separate the style from the control (just add a key and put it in your Window.Resources) and then apply the style to the control:
<Style x:Key="RoundButton_Style" TargetType="Button">
    <Style.Setters>
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border CornerRadius="10,10,10,10" BorderThickness="1,1,1,1" BorderBrush="Black"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

Open in new window

Style roundButton_Style = FindResource("RoundButton_Style") as Style;
Button myButton = new Button() { Style = roundButton_Style };

Open in new window


Regards.
0
 

Author Comment

by:tommym121
ID: 39255759
I got this error

when I put the <Style> inside the <Window>

C:\Users\Eugene\documents\visual studio 2010\Projects\TempoAssist2\TestWPF\MainWindow.xaml(5,12): error MC3023: The Key attribute can only be used on a tag contained in a Dictionary (such as a ResourceDictionary). Line 5 Position 12.
0
 
LVL 13

Accepted Solution

by:
jonnidip earned 500 total points
ID: 39255764
As I suggested, you should create a Windows.Resources section and put your style inside that section:
<Window.Resources>
    <ResourceDictionary x:Name="resourceDictionary">
        (your style here)
    </ResourceDictionary>
</Window.Resources>

Open in new window

0
 

Author Closing Comment

by:tommym121
ID: 39258303
Thanks.
0
 
LVL 13

Expert Comment

by:jonnidip
ID: 39258394
You're welcome.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 38
VB.NET (2008) - Refactoring Question 2 21
Hey!! 5 33
Adding bootstrap to only Content page and NOT master page 1 13
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question