2012-06-18 210 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來解決這個問題。