這是我有:我需要一點幫助,我的邏輯在這裏。如何在Click上有圖片選擇更改圖像?
private void HeroMouseEnter(object sender, MouseEventArgs e)
{
//I did things this was because it is easier to maintain code in a sense that is is a generic
//method made for all of the heroes images.
((Image)sender).Source = GetGlowingImage(((Image)sender).Name);
}
private void HeroMouseLeave(object sender, MouseEventArgs e)
{
//I did this IF conditional because I want to ask, "If the sender object
//is the hero selected, then do NOT change the image to normal.
if (SelectedHero != ((Image)sender).Name)
{
//I did things this was because it is easier to maintain code in a sense that is is a generic
//method made for all of the heroes images.
((Image)sender).Source = GetNormalImage(((Image)sender).Name);
}
}
private void HeroMouseClick(object sender, MouseEventArgs e)
{
if (!HasSelectedAHeroBefore)
{
HasSelectedAHeroBefore = true;
//Created a generic way to play the announcers voice according to where a user clicked.
string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
soundPlayer.Play();
//I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true
//to keep it highlighted.
SelectedHero = ((Image)sender).Name;
}
else if (HasSelectedAHeroBefore)
{
//Created a generic way to play the announcers voice according to where a user clicked.
string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
soundPlayer.Play();
//I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true
//to keep it highlighted.
SelectedHero = ((Image)sender).Name;
PreviousSelectedHero = ((Image)sender).Name;
}
}
的我想要什麼的cliffnotes。 當用戶在我的照片周圍移動鼠標時,我想要照片發光。我通過在MouseEnter上將圖像更改爲photoshopped(帶發光)來實現此目的。在MouseLeave上,我將圖片切換回正常狀態。
當用戶點擊時,我想讓點擊的圖片留在我製作的發光圖片中。所有的時候,當用戶移動他的鼠標時,我仍然希望他們在MouseEnter上發光,在MouseLeave上發光。最後,如果用戶點擊與選定的圖片不同的圖片,點擊的圖片必須保持被選中(發光),如前所述。
我很難過,我確信這是一個簡單的修復,我只是有點生疏。
非常感謝您的幫助。 :D
編輯:Aplogise,我忘了提及什麼是不工作。所有字都是我想要的,但是當我點擊另一個圖像時,前一個圖像保持發光(像選中的那樣),直到我在它上面輸入鼠標並離開它。
編輯2:我添加了一些可能工作。但我不知道如何通過名稱來選擇圖像控件。任何幫助?
else if (HasSelectedAHeroBefore)
{
//Created a generic way to play the announcers voice according to where a user clicked.
string soundfile = "AnnouncerVoice/" + ((Image)sender).Name + ".mp3";
soundPlayer.Open(new Uri(soundfile, UriKind.Relative));
soundPlayer.Play();
//I call the MouseEnter event in order to have the clicked picture glow and set the Selected bool to true
//to keep it highlighted.
PreviousSelectedHero = SelectedHero;
//Here I want to select the Image control by it's Name property. But it says I can't convert string to Image. ANy help?
GetNormalImage(((Image)PreviousSelectedHero).Name);
SelectedHero = ((Image)sender).Name;
}
您目前的解決方案存在哪些問題?哪部分不按照你想要的方式工作? – 2009-12-18 14:48:23
我編輯了我的問題。謝謝您的幫助。 :D – 2009-12-18 15:22:39
您可以像這樣在XAML中命名您的控件: 然後通過代碼中的名稱將其命名。例如「this.ImageControl1.Source = GetNormalImage((Image1).Name); –
2009-12-18 16:13:48