2011-12-07 27 views
6

我无法将MouseOver样式应用于ContentPresenter中的路径。将样式应用于ContentPresenter中的路径(BasedOn不工作!)

我有一个包含ContentPresenter按钮样式:

<Style x:Key="ContentButton" TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <ContentPresenter 
       x:Name="contentPresenter" 
       Content="{TemplateBinding Content}"> 
        <ContentPresenter.Resources> 
        <Style TargetType="{x:Type Path}" 
         BasedOn="{StaticResource ContentButtonPathStyle}"/> 
        </ContentPresenter.Resources> 
       </ContentPresenter> 

这是一种风格,所以我可以对路径翻转效果:

<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}"> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Fill" Value="#FF00FF10"/> 
      <Setter Property="Stroke" Value="Red"/> 
      <Setter Property="StrokeThickness" Value="6"/> 
     </Trigger> 
    </Style.Triggers> 
    <Setter Property="Stroke" Value="Red"/> 
    <Setter Property="Fill"> 
     <Setter.Value> 
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
       <GradientStop Color="#FFB4B3E7" Offset="0"/> 
       <GradientStop Color="#FF0800FF" Offset="1"/> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
</Style> 

我也有一个图标资源文件用一个包含路径的视图框:

<Viewbox x:Key="MyIcon"> 
    <Grid> 
     <Path Data="M78,296 L37.5,306.5 45.5,354.5 123.5,343.5 z" /> 
    </Grid> 
</Viewbox> 

最后,我创建一个按钮并将Vi ewbox资源内容:

<Button Style="{DynamicResource ContentButton}"> 
    <ContentPresenter Content="{DynamicResource MyIcon}"/> 
</Button> 

使用“支持算法FMP”样式的ContentPresenter的内容是一种技术,我发现这里:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/412b1747-60e9-4b9a-8f8f-bd56f3aff875/

但是,这是行不通的对我来说......我花了好几个小时试图弄清楚这一点!

任何想法?

谢谢!


好的基于麦克的优秀答案,这里是我最终的XAML。

我还为IsPressed添加了一个DataTriggeer,它非常棒!

我希望这可以帮助别人......

首先,样式:

<Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding RelativeSource= 
       {RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True"> 
       <Setter Property="Fill" Value="Yellow"/> 
       <Setter Property="Stroke" Value="Blue"/> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding RelativeSource= 
       {RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsPressed}" Value="True"> 
       <Setter Property="Fill" Value="Red"/> 
       <Setter Property="Stroke" Value="Black"/> 
      </DataTrigger> 
     </Style.Triggers> 
     <Setter Property="Fill" Value="Green"/> 
     <Setter Property="Stroke" Value="Red"/> 
    </Style> 

接下来,图标本身:

<Viewbox Stretch="Fill" x:Shared="False" x:Key="MyIcon"> 
     <Path StrokeThickness="6" Data="M160.26077,0.5 L196.5,36.739223 232.73923,0.5 251.12399,18.884777 214.88478,55.124001 251.12399,91.363222 232.73923,109.748 196.5,73.508779 160.26077,109.748 141.87601,91.363222 178.11522,55.124001 141.87601,18.884777 z" Stretch="Fill"/> 
    </Viewbox> 

然后,模板:

<Style x:Key="ContentButton" TargetType="{x:Type Button}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <ControlTemplate.Resources> 
         <Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/> 
        </ControlTemplate.Resources> 
        <Grid Background="Transparent"><ContentPresenter /></Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

最后,让我们把一个使用模板和样式几个按钮:

<Grid> 
    <Button Style="{DynamicResource ContentButton}" HorizontalAlignment="Left" Width="128" Height="128" VerticalAlignment="Top" Margin="85.5,87,0,0"> 
     <ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/> 
    </Button> 
    <Button Style="{DynamicResource ContentButton}" Height="64" VerticalAlignment="Top" Margin="0,87,204.5,0" HorizontalAlignment="Right" Width="64"> 
     <ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/> 
    </Button> 
    <Button Style="{DynamicResource ContentButton}" Height="96" VerticalAlignment="Bottom" Margin="234,0,0,66.5" HorizontalAlignment="Left" Width="96"> 
     <ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/> 
    </Button> 
    <Button Style="{DynamicResource ContentButton}" Height="32" VerticalAlignment="Bottom" Margin="0,0,138.5,130.5" HorizontalAlignment="Right" Width="32"> 
     <ContentPresenter Content="{DynamicResource MyIcon}" d:IsLocked="True"/> 
    </Button> 
</Grid> 

回答

7

的问题是不是“支持算法FMP”,你可以定义整个风格,而不是使用baseon,它仍然无法正常工作。你只需要移动你的窗框中的ControlTemplate资源,这将肯定

 <Style x:Key="ContentButton" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <ControlTemplate.Resources> 
          <Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/> 
         </ControlTemplate.Resources> 
         <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}"> 
         </ContentPresenter> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

工作,说实话,我不知道为什么它不ContentPresenter资源:)

内部组件编辑

如果你要根据你需要ismouseover属性绑定到一个按钮和移动你的风格里面路径集合按钮鼠标悬停改变路径的风格,见下文

 <Style x:Key="ContentButtonPathStyle" TargetType="{x:Type Path}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding RelativeSource= 
        {RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsMouseOver}" Value="True"> 
        <Setter Property="Fill" Value="#FF00FF10"/> 
        <Setter Property="Stroke" Value="Red"/> 
        <Setter Property="StrokeThickness" Value="6"/> 
       </DataTrigger> 
      </Style.Triggers> 
      <Setter Property="Stroke" Value="Red"/> 
      <Setter Property="Fill"> 
       <Setter.Value> 
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
         <GradientStop Color="#FFB4B3E7" Offset="0"/> 
         <GradientStop Color="#FF0800FF" Offset="1"/> 
        </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
     </Style> 


     <Style x:Key="ContentButton" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <ContentPresenter x:Name="contentPresenter" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 


     <Viewbox x:Key="MyIcon"> 
      <Grid Background="Transparent"> 
       <Grid.Resources> 
        <Style TargetType="{x:Type Path}" BasedOn="{StaticResource ContentButtonPathStyle}"/> 
       </Grid.Resources> 
       <Path Data="M78,296 L37.5,306.5 45.5,354.5 123.5,343.5 z" /> 
      </Grid> 
     </Viewbox> 

而只是让你知道,这是相当无用的基础风格到另一个,并添加什么都没有,你可以使用:

<Path Style="{StaticResource ContentButtonPathStyle}" Data="...." /> 

希望这有助于

+0

+1,这肯定能行!顺便说一句,另一个解决方案是包装与数据模板的视框,并将其分配给按钮的contenttemplate,我也不知道为什么绑定内容不起作用。 :( –

+0

使用适用于我! 还有一个问题:现在,鼠标悬停功能只能用于路径本身...而不是视图框/按钮!因此,当鼠标点击零件时,鼠标悬停会闪烁 有没有办法让鼠标悬停在按钮上,而不仅仅是路径上吗? 谢谢! – user1084857

+0

我编辑了我的答案,看它是否适用于你 – MaRuf