u013981858
CFirst
2015-09-19 08:37
采纳率: 100%
浏览 2.6k
已采纳

WPF自定义按钮问题 -- 样式相同图片不同

 <UserControl x:Class="WpfApplication6.LeftButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style x:Key="ListLeftButton" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle x:Name="rectangle">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="skin/01228[46x62x8BPP].png"/>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="Fill" TargetName="rectangle">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="skin/01227[46x62x8BPP].png"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True"/>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Fill" TargetName="rectangle">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="skin/01226[46x62x8BPP].png"></ImageBrush>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <StackPanel>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
        <Button Style="{StaticResource ListLeftButton}" Width="50" Height="60"/>
    </StackPanel>
</UserControl>

我利用模板自定义了按钮,这些按钮的样式都相同只是背景图片不一样,但我只会在模板中定义一张,要不同的按钮对应不同的图片应该怎么做呐?求解答,多谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • u013981858
    CFirst 2015-09-19 09:16
    已采纳

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="300">

    <br> <Setter Property="Template"><br> <Setter.Value><br> <ControlTemplate TargetType="{x:Type Button}"><br> <Grid><br> <Rectangle x:Name="rectangle"><br> <Rectangle.Fill><br> <ImageBrush ImageSource="skin/01228[46x62x8BPP].png"/><br> </Rectangle.Fill><br> </Rectangle><br> </Grid><br> <ControlTemplate.Triggers><br> <Trigger Property="IsFocused" Value="True"><br> <Setter Property="Fill" TargetName="rectangle"><br> <Setter.Value><br> <ImageBrush ImageSource="skin/01227[46x62x8BPP].png"/><br> </Setter.Value><br> </Setter><br> </Trigger><br> <Trigger Property="IsDefaulted" Value="True"/><br> <Trigger Property="IsMouseOver" Value="True"/><br> <Trigger Property="IsPressed" Value="True"><br> <Setter Property="Fill" TargetName="rectangle"><br> <Setter.Value><br> <ImageBrush ImageSource="skin/01226[46x62x8BPP].png"></ImageBrush><br> </Setter.Value><br> </Setter><br> </Trigger><br> <Trigger Property="IsEnabled" Value="False"/><br> </ControlTemplate.Triggers><br> </ControlTemplate><br> </Setter.Value><br> </Setter><br>









    
    

    效果如图
    图片说明

    我想让每张图片都不一样怎么做

    点赞 评论
  • weixin_31431997
    weixin_31431997 2015-09-19 09:45

    图片说明图片说明我什么都不懂

    点赞 评论

相关推荐