开发者

WPF Set value by trigger when using binding

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>
         开发者_如何学JAVA       <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???


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜