Solved

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

Posted on 2013-06-17
5
1,186 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0
 
LVL 13

Expert Comment

by:jonnidip
Comment Utility
You're welcome.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now