tags:

views:

23

answers:

2

hello,

i narrowed down what i want my wpf button to look like using XAML.

now i would like to create a sub classed button control that i can just re-use w/out having to write all that markup

<Button Click="TestGridColumnButton_Click" Background="Transparent" Width="16" Height="16" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}">
    <Button.Template>
        <ControlTemplate>
            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource SourceStyle}" />
        </ControlTemplate>
    </Button.Template>
</Button>

how can i set all these properties using C# ?

A: 

Hi,

There are multiple ways to do that :

[Note: For the sake of this answer, I assume you are creating a button in a simple window.]

1) A not-reusable and kind of dirty way would be to assign all the properties in the window's loaded event. By naming the button object, you can get reference to it and assign values to the properties, register to the event and assign a template. I assume you were having trouble with the template properties. To create a template in code, you can either use FrameworkElementFactory class (which is deprecated) or use XamlReader.Load method.(which is the preferred approach).

2) Create a custom control, for which a custom template is created in generic.xaml file. You can name elements in the custom template and retrieve references to them by overriding OnApplyTemplate method. Then you can create public properties which directly map to the Image element in the template or expose the Image element as a public property in the control. Using this approach, you can set properties in C# code using your exposed properties.

3) You can also define multiple styles and toggle between them in c# code. This method works just like in HTML when we toggle classes using JavaScript.

Hope this helps!

decyclone
Here is the reference to the Xaml.Load method : http://msdn.microsoft.com/en-us/library/ms590388.aspx
decyclone
cool thanks.. yah i remembered that a style could be created with a template side of it.. i just never did that :)
Sonic Soul
+2  A: 

Create a style for your button and add to a global resource dictionary.

<Application x:Class="TestApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="MyIconButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent" />
                <Setter Property="Width" Value="16" />
                <Setter Property="Height" Value="16" />
                <Setter Property="Margin" Value="0,0,0,0" />
                <Setter Property="Padding" Value="0,0,0,0" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="buttonImage.jpg"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Use the style like this:

  <Button Style="{StaticResource MyIconButtonStyle}" Click="Button_Click"/>
Wallstreet Programmer