我無法將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>
+1,這肯定能行!順便說一句,另一個解決方案是包裝與數據模板的視框,並將其分配給按鈕的contenttemplate,我也不知道爲什麼綁定內容不起作用。 :( –
使用適用於我! 還有一個問題:現在,鼠標懸停功能只能用於路徑本身...而不是視圖框/按鈕!因此,當鼠標點擊零件時,鼠標懸停會閃爍 有沒有辦法讓鼠標懸停在按鈕上,而不僅僅是路徑上嗎? 謝謝! –
user1084857
我編輯了我的答案,看它是否適用於你 – MaRuf