I have created a button in Expression Blend 4. I want to dynamically create instances of this button at run time.
The code for the button is below:
<Button Content="Button" HorizontalAlignment="Left" Height="139" Margin="46,107,0,0" VerticalAlignment="Top" Width="412" Grid.ColumnSpan="2">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
</Button>
In addition to the supplying the code can you put comments in explaining what you are doing so that I can learn the general principle.
I know this is a simple question so I have been reading up places such as: Expression blend & WPF, Is there a way to "extract" WPF controls of Expression Blend?, and http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ffa981b8-9bba-43a2-ab5e-8e59bc10fc0d/ unfortunately none of these have helped.
In your WPF application you should have a
App.xamlfile, in there you can addStylesthat are to be used thoughout your UI.Example:
Or if you dont want to apply to all buttons, you can give your
StyleaKeyso you can apply to certain Buttons in your UITo use this
Styleon aButton, just add a binding to theStyleproperty of theButtonthis will apply the
Stylejust to thisButtonOr if you really want to do it in code behind you can just add the
Brushyou want to the backgroundits very easy to translate xaml to code because xaml uses the exact same property names, like the code brush I posted above:
is ….
Xaml just accesses properties in the button, they will all have the same names in C#.