2012-06-18 211 views
2

是否可以从另一资源更改资源。如果鼠标位于StartButtonMain之上,我喜欢更改StartButtonRed的背景。从资源触发资源

<ImageBrush x:Key="RedBackgroundActive" ImageSource="/Images/start_red_active.png" Stretch="Fill"/> 

<Style x:Key="StartButtonMain" TargetType="{x:Type local:SimpleButton}"> 
    <Style.Resources> 
     <ImageBrush x:Key="MainBackground" ImageSource="/Images/start_main_normal.png" Stretch="Fill"/> 
     <ImageBrush x:Key="MainBackgroundActive" ImageSource="/Images/start_main_active.png" Stretch="Fill"/> 
    </Style.Resources> 
    <Style.Setters> 
     <Setter Property="HorizontalAlignment" Value="Stretch"/> 
     <Setter Property="VerticalAlignment" Value="Stretch"/> 
     <Setter Property="Background" Value="{StaticResource MainBackground}"/> 
     <Setter Property="Visibility" Value="Visible"/> 
    </Style.Setters> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{StaticResource MainBackgroundActive}"/> 
      // Change the background of StartButtonRed to RedBackgroundActive 
     </Trigger> 
    </Style.Triggers> 
</Style> 

<Style x:Key="StartButtonRed" TargetType="{x:Type local:SimpleButton}"> 
    <Style.Resources> 
     <ImageBrush x:Key="RedBackground" ImageSource="/Images/start_red_normal.png" Stretch="Fill"/> 
    </Style.Resources> 
    <Style.Setters> 
     <Setter Property="HorizontalAlignment" Value="Stretch"/> 
     <Setter Property="VerticalAlignment" Value="Stretch"/> 
     <Setter Property="Background" Value="{StaticResource RedBackground}"/> 
     <Setter Property="Visibility" Value="Visible"/> 
    </Style.Setters> 
</Style> 
+0

我不确定这是否正是您想要的,但请查看:http://msdn.microsoft.com/en-us/library/ms745683.aspx,具体请查看“扩展样式”一节“和”BasedOn“属性。 – CodingGorilla

+0

@编码大猩猩不,但很高兴知道BasedOn属性。 –

回答

0

不能做到这一点。因为样式是资源,并且触发器只能从其自己的Template中定位FrameworkElements,或者使用绑定到其他实际的FrameworkElement,所以无法绑定到其他资源。

你可以用UserControl Triggers来解决这个问题。