WPF Label的content或Background也可以加载放在canvas(path)内的自定义图片。避免用(button+content+canvas+path)多次点击造成资源浪费。

方法一:Button 方式(有点亮按钮的背景,即边缘有颜色变化)

<Button Height="48" Width="1920" Content="{StaticResource StutasBarBackground}"/>

方法二:(无点亮按钮的背景,消耗资源大)

 <Button Height="48" Width="1920" Content="{StaticResource StutasBarBackground}" Style="{StaticResource Normal_Button}"/>

其中,Normal_Button:

<Style x:Key="Normal_Button" TargetType="{x:Type Button}">
        --><!--<Setter Property="Width" Value="153"/>
        <Setter Property="Height" Value="60"/>--><!--
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border   Name="border" BorderThickness="0" BorderBrush="Transparent">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <VisualBrush Visual="{Binding  Path=Content, RelativeSource={RelativeSource TemplatedParent}}"></VisualBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

方法三:(无点亮按钮的背景,消耗资源小)Label避免Button方式下的多次点击、避免鼠标在Button悬停状态下点亮按钮(因为我不需要点亮功能、点击功能、触发功能)

 <Label Content="{StaticResource StutasBarBackground}" Height="48" Width="1920"  />

其中,StutasBarBackground表示自定义的图片:

    <Canvas Width="1920" Height="84" x:Key="StutasBarBackground">
        <Rectangle Height="48" Width="1920" Fill="#FFC1C8CE" Margin="0" StrokeThickness="1.5"/>
    </Canvas>

方法四 Label 的background加载(效果最好,可以通过Width或height自适应调整背景,可以给其他不同长度换宽度的Label使用

<Label Height="70" Width="676" Canvas.Left="0" Canvas.Top="0">
                <Label.Background>
                    <VisualBrush Visual="{StaticResource PublicListx41}"/>
                </Label.Background>
            </Label>

其中,PublicListx41表示放在容器Canvas中的数据.。效果如下:

总结:方法四最好,既可以达到自己想要的目的,又可以服用背景PublicListx41的数据,也可以降低内存的消耗。

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/83150481