我需要一点帮助,我的逻辑在这里。如何在Click上有图片选择更改图像?
这是我有:我需要一点帮助,我的逻辑在这里。如何在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;
}
可以尝试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或网格,甚至是某种列表)。鉴于这取决于以某种方式存储的图像,并且我将它从头顶写下来,它可能会或可能不会有用。这就是说应该选择你想改变的图像,并将它改回原来的。
好的,你的回答引发了我的头脑中的解决方案,尽管如此,请检查我的编辑: – 2009-12-18 15:35:02
我可以看到你的Xaml?它看起来像你可能只是有一系列的
姆明图片直接在XAML中设置图片从我在我的解决方案资源管理器中创建的文件夹中拉出,并手动添加.gifs – 2009-12-18 15:58:33
至于创建多个图像的选择,你可以使用触发器和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>
我解决这个问题是这样的(伪代码):
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。
您目前的解决方案存在哪些问题?哪部分不按照你想要的方式工作? – 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