如何重用行为?

时间:2015-06-01 16:16:53

标签: xaml windows-store-apps

根据容器按钮的IsEnabled属性,有两个行为可以改变路径的不透明度。我有几个按钮,我想重用这两个行为,因为它们的容器具有相同的路径。我该怎么做?

<Button x:Name="buttonConcentration">
    <Canvas Width="42.6667" Height="42.6667">
        <Path Opacity="0.2" Width="42.835" Height="42.696" Stretch="Fill" 
        Data="..." UseLayoutRounding="False">
            <Interactivity:Interaction.Behaviors>
                <Core:DataTriggerBehavior 
                    Binding="{Binding IsEnabled,  ElementName=buttonConcentration}" 
                    Value="False">
                    <Core:ChangePropertyAction PropertyName="Opacity" Value="0.2"/>
                </Core:DataTriggerBehavior>
                <Core:DataTriggerBehavior 
                    Binding="{Binding IsEnabled, ElementName=buttonConcentration}" 
                    Value="True">
                    <Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Path>
    </Canvas>
</Button>

1 个答案:

答案 0 :(得分:1)

尝试在Window.Resources上进行设置。

也许通过这段代码,您将能够达到您的期望:

<Window.Resources>
    <Button x:Key="myButtonConcentration" x:Shared="False">
        <Canvas Width="42.6667" Height="42.6667">
            <Path Opacity="0.2" Width="42.835" Height="42.696" Stretch="Fill" 
            Data="..." UseLayoutRounding="False">
                <Interactivity:Interaction.Behaviors>
                    <Core:DataTriggerBehavior 
                        Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}"
                        Value="False">
                        <Core:ChangePropertyAction PropertyName="Opacity" Value="0.2"/>
                    </Core:DataTriggerBehavior>
                    <Core:DataTriggerBehavior 
                        Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}"
                        Value="True">
                        <Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
                    </Core:DataTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </Path>
        </Canvas>
    </Button>
</Window.Resources>

<Grid>
    <ContentControl Name="MyButton1" Content="{StaticResource myButtonConcentration}" />
    <ContentControl Name="MyButton2" Content="{StaticResource myButtonConcentration}" />
</Grid>