Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a Button template programatically
    text
    copied!<p>First, I'm sorry about my English I hope you understand my problem (Corrections accepted and the next post will be more correct).</p> <p>OK, this is my scenario: Developing for Windows Phone 7. I can't use XAML templates. I need to create a template at run time with C# code. The only elements that I have to customize are the background and foreground colors in inactive state and when the user holds the button, and a rounded corner for the border.</p> <p>Setting up the <code>Button.Style</code> property works only for the default state (inactive) and I can't create a corner radius:</p> <pre><code>Style buttonStyle = new Style(typeof(Button); buttonStyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Colors.Blue)); buttonStyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush(Colors.White))); Button myBytton = new Button(); myButton.Style = buttonStyle; </code></pre> <p>The next step was to create a CustomTemplate for the Button.Template property, but always raises an Exception:</p> <pre><code>CustomTemplate myTemplate = new CustomTemplate(); myTemplate.TargetType = typeof(Button); // Exception raises here: InvalidOperationException wihtout more info </code></pre> <p>(..Continues code ...)</p> <pre><code>Button myBytton = new Button(); myButton.Template = myTemplate; </code></pre> <p>All examples I've viewed used XAML for the CutomTemplate. I don't know how to setup styles for different button states and for the border radius.</p> <p>If anybody can help me... Thanks for reading,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload