Solved

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

Posted on 2013-06-17
5
1,211 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

23 Experts available now in Live!

Get 1:1 Help Now