从资源触发资源

时间:2012-06-18 20:07:25

标签: c# wpf

是否可以从其他资源更改资源。如果鼠标在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>

1 个答案:

答案 0 :(得分:0)

不能这样做。由于样式是资源,触发器只能从自己的模板中定位FrameworkElements或使用绑定到其他实际的FrameworkElement,因此无法绑定到其他资源。

您可以使用UserControl Triggers解决此问题。

相关问题