简体   繁体   English

WPF C# 更改 hover 上的按钮图像

[英]WPF C# change a button image on hover

Hello I'm new to xaml and c# and I'm trying to create a application where a listbox show the list of object.您好,我是 xaml 和 c# 的新手,我正在尝试创建一个应用程序,其中列表框显示 object 的列表。 This part work mostly fine.这部分工作正常。 However i do find some trouble with the fact that my button are custom with (normally the image background changing on hover).但是,我确实发现了一些问题,因为我的按钮是自定义的(通常图像背景在悬停时发生变化)。

I do have a probleme with the button showing only on the last element of the list.我确实对仅在列表的最后一个元素上显示的按钮有疑问。 Moreover the button do appear if i modify my code while the app is running but if i rerun the app the same code does not work anymore.此外,如果我在应用程序运行时修改我的代码,但如果我重新运行应用程序,相同的代码将不再工作,该按钮确实会出现。

Finally, i do get a warning saying that i can't define a propety on the element of a propety in one of those 4 lines最后,我确实收到一条警告,说我无法在这 4 行之一中的属性元素上定义属性

<Style TargetType="{x:Type Button}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="Button">

here is the code of the xaml window (i suppose the problem is in the button part so i only put it, but if you need the rest of the code just ask)这是xaml window的代码(我想问题出在按钮部分,所以我只放了它,但是如果您需要代码的rest,请询问)

<Button x:Name="sell" BorderThickness="0" Background="Transparent" HorizontalAlignment="Center"  Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"  Grid.ColumnSpan="1" Cursor="Hand" MaxWidth="250">
                            <Button.Style>
                                <Style TargetType="{x:Type Button}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="Button">
                                                <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Width}" x:Name="panel">
                                                    <ContentPresenter Width="{TemplateBinding Width}" Height="68" />
                                                </StackPanel>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="IsPressed" Value="true">
                                                        <Setter Property="Background" Value="Transparent" TargetName="panel" />

                                                    </Trigger>
                                                    <Trigger Property="IsMouseOver" Value="true">
                                                        <Setter Property="Background" Value="Transparent" />
                                                        <Setter Property="Content">
                                                            <Setter.Value>
                                                                <Image Source="{Binding lienDeletePressed}"/>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Trigger>
                                                    <Trigger Property="IsMouseOver" Value="false">
                                                        <Setter Property="Content">
                                                            <Setter.Value>
                                                                <Image Source="{Binding lienDelete}"/>
                                                            </Setter.Value>
                                                        </Setter>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Button.Style>
                        </Button>

And here is the c# file这是 c# 文件

namespace Planet_manager
{
    /// <summary>
    /// Logique d'interaction pour batiments.xaml
    /// </summary>
    public partial class batiments : Window
    {
        public batiments()
        {
            InitializeComponent();
            List<Batiment> listBats = new List<Batiment>();
            listBats.Add(new Batiment() { Nom = "Complexe immobilier", Niveau = 1, img = "/imgs/bats/img_bat1.jpg", Prix_bois = 10, Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned=1});
            listBats.Add(new Batiment() { Nom = "Scierie", Niveau = 0, img = "/imgs/bats/img_bat2.jpg", Prix_fer = 10, Prix_or = 1000, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Mine de fer", Niveau = 1, img = "/imgs/bats/img_bat3.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 100, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Piscine", Niveau = 1, img = "/imgs/bats/img_bat4.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Immeuble d'habitation", Niveau = 5, img = "/imgs/bats/img_bat5.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Caserne", Niveau = 1, img = "/imgs/bats/img_bat6.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 500, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Complexe immobilier", Niveau = 1, img = "/imgs/bats/img_bat1.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Scierie", Niveau = 5, img = "/imgs/bats/img_bat2.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Mine de fer", Niveau = 1, img = "/imgs/bats/img_bat3.jpg", Prix_fer = 10, Prix_or = 100000, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Piscine", Niveau = 1, img = "/imgs/bats/img_bat4.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Immeuble d'habitation", Niveau = 1, img = "/imgs/bats/img_bat5.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Caserne", Niveau = 5, img = "/imgs/bats/img_bat6.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Complexe immobilier", Niveau = 1, img = "/imgs/bats/img_bat1.jpg", Prix_fer = 10, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Scierie", Niveau = 5, img = "/imgs/bats/img_bat2.jpg", Prix_fer = 1000, Prix_or = 0, Prix_petrole = 0, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Mine de fer", Niveau = 10, img = "/imgs/bats/img_bat3.jpg", Prix_fer = 10, Prix_or = 110, Prix_petrole = 1110, owned = 1 });
            listBats.Add(new Batiment() { Nom = "Piscine", Niveau = 10, img = "/imgs/bats/img_bat4.jpg", Prix_fer = 10, Prix_or = 5, Prix_petrole = 0, owned = 1 });

            Jeu j = new Jeu() { Id = 1, Nom_joueur = "samuel", Nom_planete = "novus", Nbre_de_tour = 1, Fer = 10, Or = 10, Bois = 10, Petrole = 0, Argent = 1000, Nbre_de_slot = 0, Nbre_slot_max = 5 };


            foreach (Batiment b in listBats)
            {
                //ajout lien construction-amelioration

                if (b.owned ==1)
                {
                    //cas ou on possède deja le batiment
                    if (b.Prix_bois > j.Bois || b.Prix_fer > j.Fer || b.Prix_or > j.Or || b.Prix_petrole > j.Petrole)
                    {
                        b.lienBuild = "/imgs/ui/buttons/button_améliorer_nope.png";
                        b.lienBuildPressed = "/imgs/ui/buttons/button_améliorer_nope.png";
                    }
                    else
                    {
                        b.lienBuild = "/imgs/ui/buttons/button_améliorer.png";
                        b.lienBuildPressed = "/imgs/ui/buttons/pressed/button_améliorer_pressed.png";

                    }
                }
                else //cas ou on veut construire un batiment
                {
                    if (b.Prix_bois > j.Bois || b.Prix_fer > j.Fer || b.Prix_or > j.Or || b.Prix_petrole > j.Petrole)
                    {
                        b.lienBuild = "/imgs/ui/buttons/button_construire_nope.png";
                        b.lienBuildPressed = "/imgs/ui/buttons/button_construire_nope.png";
                    }
                    else
                    {
                        b.lienBuild = "/imgs/ui/buttons/button_construire.png";
                        b.lienBuildPressed = "/imgs/ui/buttons/pressed/button_construire_pressed.png";
                    }
                }

                //ajout lien destruction
                if (b.owned == 1 )
                {
                    b.lienDelete = "/imgs/ui/buttons/button_detruire.png";
                    b.lienDeletePressed = "/imgs/ui/buttons/pressed/button_detruire_pressed.png";
                }
                else
                {
                    b.lienDelete = "";
                    b.lienDeletePressed = "";
                }


            }

            list_bat.ItemsSource = listBats;
        }


    }


}

thanks for your help感谢您的帮助

here the picture of the result at launch: bug printing and here after i just deleted saved, then added again the这里是启动时的结果图片:错误打印,在我刚刚删除后保存,然后再次添加

<Image Source="{Binding lienDeletePressed}"/>

bug printing ok错误打印正常

here is the code of the batiments class as requested:这里是要求的 batiments class 的代码:

class Batiment
    {

        public int Id { get; set; }
        public string Nom { get; set; }
        public string img { get; set; }
        public int Niveau { get; set; }
        public int Ressources_produites { get; set; }
        public int Prix_bois { get; set; }
        public int Prix_fer { get; set; }
        public int Prix_or { get; set; }
        public int Prix_petrole { get; set; }
        public int Quantite_pop { get; set; }
        public int Qualie_pop { get; set; }
        public int owned { get; set; }
        public int prod_pop { get; set; }
        //valeur des liens des boutons construire/ameliorer
        public string lienBuild { get; set; }
        //valeur des liens des boutons detruire
        public string lienDelete { get; set; }
        public string lienBuildPressed { get; set; }
        //valeur des liens des boutons detruire
        public string lienDeletePressed { get; set; }
}

I did some others tests and finally i changed the button and now it work fine.我做了一些其他的测试,最后我改变了按钮,现在它工作正常。 The only downside is that the basic "blue system" color of the button on hover is still there and i couldn't make the code hiding it without breaking the image binding.唯一的缺点是 hover 上按钮的基本“蓝色系统”颜色仍然存在,我无法在不破坏图像绑定的情况下隐藏它的代码。


                        <Button x:Name="buy" Tag="{Binding Id}" BorderThickness="0" Background="Transparent" HorizontalAlignment="Center"  Grid.Row="3"  Grid.Column="0" VerticalAlignment="Center"  Grid.ColumnSpan="1" Cursor="Hand" MaxWidth="250" Click="buy_Click" >
                            <Image>
                                <Image.Style>
                                    <Style TargetType="{x:Type Image}">
                                        <Setter Property="Source" Value="{Binding lienBuild}"/>
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Source" Value="{Binding lienBuildPressed}"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                        </Button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM