locked
Button.ContentTemplate - How to create a new non binding Resource? RRS feed

  • Question

  • I've created a template for a button, borrowing from an existing DataTemplate resource in StandardStyles.xaml

    I'd like to make this a Resource so I can apply it to a number of buttons on my page, but they won't be data bound. I just want to inject four values (currently marked up as Bindings) into each of my styled buttons.

    How would I go about removing the {Bindings} in the ContentTemplate and passing the property values from my buttons please? Do I need a custom control? Or is there a way I can extend Button? Or have I missed an easier way entirely?

    <Button x:Name="Button1">
        <Button.ContentTemplate>
            <DataTemplate>
                <Grid Height="110" Width="308" Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
                        <Image Source="{Binding IMAGESOURCE}" Stretch="UniformToFill"/>
                    </Border>
                    <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
                        <TextBlock Text="{Binding TITLETEXT}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/>
                        <TextBlock Text="{Binding DESCTEXT}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </Button.ContentTemplate>
    </Button>


    • Edited by RD8388 Wednesday, September 25, 2013 4:22 PM
    Wednesday, September 25, 2013 4:19 PM

Answers

  • Databinding is the only way to inject values into your template. You can bind either to properties on the Button or to a separate data context. It sounds like the latter is what you want to avoid.

    You can use attached properties to add new properties to your button which your template can then bind to. You could also create a new class derived from Button

    See Attached properties overview .

    --Rob

    • Marked as answer by RD8388 Monday, September 30, 2013 10:58 AM
    Wednesday, September 25, 2013 6:17 PM
    Moderator