views:

353

answers:

1

Why will not this work:

    <Button Width="200" Height="50">
    <Button.Style>
    <Style TargetType="Button">
        <Setter Property="Height" Value="{Binding RelativeSource={RelativeSource Self}, Path=Height}"/>
        <Setter Property="Background" Value="Blue"/>
        <Style.Triggers>
                <Trigger Property="Button.IsPressed" Value="true">
                    <Setter Property="Background" Value="green"/>
                    <Setter Property="Height" Value="20"/>
                </Trigger>
        </Style.Triggers>
    </Style>
    </Button.Style>

<Button.Template>
    <ControlTemplate>
        <Canvas x:Name="MainCanvas" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
            <Border 
            Width="{TemplateBinding Width}"  Height="{TemplateBinding Height}"
                      x:Name="Border"  
                      CornerRadius="2" 
                      BorderThickness="1"
                      Background="{TemplateBinding Background}"
                      BorderBrush="black">
               <ContentPresenter 
                 Margin="2"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 RecognizesAccessKey="True"/>
             </Border>
        </Canvas>
    </ControlTemplate>
</Button.Template>
Hello
</Button>

Like this:

<Button Width="200" >
<Button.Style>
    <Style TargetType="Button">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Background" Value="Blue"/>
        <Style.Triggers>
                <Trigger Property="Button.IsPressed" Value="true">
                    <Setter Property="Background" Value="green"/>
                    <Setter Property="Height" Value="20"/>
                </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>
<Button.Template>
    <ControlTemplate>
        <Canvas x:Name="MainCanvas" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
            <Border 
            Width="{TemplateBinding Width}"  Height="{TemplateBinding Height}"
                      x:Name="Border"  
                      CornerRadius="2" 
                      BorderThickness="1"
                      Background="{TemplateBinding Background}"
                      BorderBrush="black">
               <ContentPresenter 
                 Margin="2"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 RecognizesAccessKey="True"/>
             </Border>
        </Canvas>
    </ControlTemplate>
</Button.Template>
Hello
</Button>  

I want the button to shrink when I press it. This is a prototype for a custom control so the Style will be lift out to a Generics.xmal files later on. And why does it not show the 'Hello' string on the Button???

+1  A: 

The string "Hello" won't show up on the button until you add TargetType="Button" to the ControlTemplate tag, because otherwise the ContentPresenter doesn't know what it's dealing with:

<ControlTemplate TargetType="Button">

As for your style binding, what you're trying to do is bind a property to itself - that doesn't make any sense. What are you trying to achieve?

Rob Fonseca-Ensor
TargetType did the trick for the first problem. Gonna do a custom control that I can set a Height on and I want the width to change on some trigger. So the will be a genric.axml file that starts like this: <Style TargetType="{x:Type local:CustomControl1}"> <Setter Property="Style"> <Style.Triggers> <Trigger ...
zwi
Do you really want the whole button to shrink (thus affecting layout of its siblings), or just to look like it's shrinking (without actually making the whole screen reflow)
Rob Fonseca-Ensor
I want the layout to be affected so if the control is in a stackpanel all the other controls will be moved when the shrinking occur
zwi
Do you mind if the text shrinks as well? Using LayoutTransform would be quite appropriate here
Rob Fonseca-Ensor
no, this is a simplier example of a more complext thing I doing so don't hang up yourself on that it's a button think of it more like a content control only
zwi