2009-12-18 45 views
0

這是我有:我需要一點幫助,我的邏輯在這裏。如何在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; 

     } 
+0

您目前的解決方案存在哪些問題?哪部分不按照你想要的方式工作? – 2009-12-18 14:48:23

+0

我編輯了我的問題。謝謝您的幫助。 :D – 2009-12-18 15:22:39

+0

您可以像這樣在XAML中命名您的控件:然後通過代碼中的名稱將其命名。例如「this.ImageControl1.Source = GetNormalImage((Image1).Name); – 2009-12-18 16:13:48

回答

1

可以嘗試HeroMouseClick ...

PreviousSelectedHero = SelectedHero; 
SelectedHero = ((Image)sender).Name; 

EDIT2:我假設的XAML看起來是這樣的:

<StackPanel Name = "myContainer> 
    <Image Name="heroOne" Source="source1.gif" /> 
    <Image Name="heroTwo" Source="source2.gid" /> 
</StackPanel> 

然後在代碼隱藏,你可以這樣做:

using System.Linq; //EDIT - you'll need this for any Linq query 

var hero = 
    (from Image i in myContainer.Children 
    where i.Name == previousSelectedHero.Name 
    select i).Single(); 
    //this selected the Image from the StackPanel "myContainer" using the Images name attribue 
    //and assigns it to hero 

hero.Source = GetNormalImage(hero.Name); 
//this should then set the deselected image back to its original source 

其中「myContainer中」是不管你已經有了存儲在所有圖像(如一個Stackpanel或網格,甚至是某種列表)。鑑於這取決於以某種方式存儲的圖像,並且我將它從頭頂寫下來,它可能會或可能不會有用。這就是說應該選擇你想改變的圖像,並將它改回原來的。

更多編輯2:我假設您正在努力的Linq聲明。 here是Linq的一個鏈接,而here是一堆樣本。

+0

好的,你的回答引發了我的頭腦中的解決方案,儘管如此,請檢查我的編輯: – 2009-12-18 15:35:02

+0

我可以看到你的Xaml?它看起來像你可能只是有一系列的標籤在XAML本身設置的源,但我真的需要知道,如果是這樣的話,或者如果你是從數據源拉圖像 – MoominTroll 2009-12-18 15:43:35

+0

姆明圖片直接在XAML中設置圖片從我在我的解決方案資源管理器中創建的文件夾中拉出,並手動添加.gifs – 2009-12-18 15:58:33

2

至於創建多個圖像的選擇,你可以使用觸發器和BitMapEffects大有作爲的這種行爲。有一個很好的tutorial here使圖像對鼠標懸停發光效果。只要圖像是「選定」的圖像,您就可以使用類似的方法與DataTrigger保持發光效果。

樣本樣式設置爲「輝光」鼠標懸停圖片:

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="BitmapEffect"> 
     <Setter.Value> 
      <OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/> 
     </Setter.Value> 
     </Setter> 
    </Trigger> 
    </Style.Triggers> 

如何使用類似的東西,以保持選定的圖像,無論發光鼠標懸停的:

<Style.Triggers> 
    <DataTrigger Binding={Binding Path=IsSelected} Value="True"> 
     <Setter Property="BitmapEffect"> 
     <Setter.Value> 
      <OuterGlowBitmapEffect GlowColor="Red" GlowSize="4"/> 
     </Setter.Value> 
     </Setter> 
    </DataTrigger> 
</Style.Triggers> 
0

我解決這個問題是這樣的(僞代碼):

class HeroControl 
{ 
    bool IsSelected {get;set;} 

    MouseEnter() 
    { 
    if (!IsSelected) 
    { 
     SetGlow(true); 
    } 
    } 

    MouseLeave() 
    { 
    if (!IsSelected) 
    { 
     SetGlow(false); 
    } 
    } 

    MouseDown() 
    { 
    IsSelected = true; 

    // May be event invocation or method call or any other way. 
    NotifySelectedHeroChanged(this); 
    } 
} 

class HeroesContainer 
{ 
    List Heroes; 

    OnCurrentHeroChanged(newHero, oldHero) 
    { 
    oldHero.IsSelected = false; 
    // update new hero if needed. 
    } 
} 

呀,比較抽象。但誰知道,也許它會幫助你以某種方式:)。

乾杯,Anvaka。