Solved

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

Posted on 2013-06-17
5
1,302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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