Bind a fill property in a path to a Foreground property from the ContentControl in a style

前端 未结 3 1953
遥遥无期
遥遥无期 2021-02-13 13:14

I have silverlight problem I\'v used two days to fight: a template with a style controls a button. In the concrete button I have a canvas with paths as content. The problem is t

相关标签:
3条回答
  • 2021-02-13 13:32

    Binding to ContentControl should help (Silverlight 5):

    <Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
        <Canvas x:Name="PlayIcon">
            <Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z" 
                  Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType=ContentControl}}" />
        </Canvas>
    </Button>
    
    0 讨论(0)
  • 2021-02-13 13:38

    Actually instead of setting Canvas as Content in Button you can put it in Templete.

          <Style x:Key="PathButtonStyle" TargetType="Button">
    
        ... Animations and state stuff
         <Canvas x:Name="PlayIcon">
        <Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z" 
              Fill="{TemplateBinding Foreground}" />
      </Canvas>
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" >
            <ContentControl x:Name="ContentContainer" 
              ContentTemplate="{TemplateBinding ContentTemplate}" 
              Content="{TemplateBinding Content}" 
              Foreground="{TemplateBinding Foreground}" />
          </Border>
        </Style>
    

    you can do anything you want if you'll use this scenario.

    0 讨论(0)
  • 2021-02-13 13:42

    Since you gave a name to your button, you can use the Binding ElementName parameter:

    <Button x:Name="Play" Style="{StaticResource PathButtonStyle}" >
        <Canvas x:Name="PlayIcon">
            <Path Data="F1M191.4839,96.1763L177.9149,106.5173L177.9149,85.9293z" 
                Fill="{Binding Foreground, ElementName=Play}" />
        </Canvas>
    </Button>
    
    0 讨论(0)
提交回复
热议问题