Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-17
5
Medium Priority
?
1,570 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

824 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