I want to use the EventHandler
MouseEnter & MouseLeave to change the OpasityMask
of the images. Some images will be generated at runtime and on different pages. The point is that I need to have access to the sender
to change property OpacityMask
on the MouseHover command in the ViewModel class .
view
this.WendActivated(d =>
{
d(this.BindCommand(ViewModel,
vm => vm.MouseHover,
v => v.myImage,
myImage.Events().MouseEnter));
});
ViewModel
private void Image_MouseEnterExecute(object sender, MouseEventArgs e)
{
PlaySound("mouse_hover.mp3");
var image = sender as Image;
image.OpacityMask = new SolidColorBrush { Opacity = 1, Color = Colors.Black };
}
I don't know how to implement ObservedChange
to change the OpacityMask
.
public ReactiveCommand MouseHover => image_MouseEnter;
public ObservedChange<object, object> ImageBrush => imageBrush;
public ViewModel()
{
//Can not convert method group to action.
image_MouseEnter = ReactiveCommand.Create(Image_MouseEnterExecute);
//Can not convert method group to action.
imageBrush = ReactiveCommand.Create(Image_MouseEnterExecute);
}
Why don't I put a Binding on the property OpacityMask
? The problem is that I have a lot of images, some created at runtime. I don't want all the Images to change at the same time, or to create a property OpacityMask
in the ViewModel class for each Image.
I hope someone with experience can help me.
Let see if this helps you...
First, change your ViewModel, try not to use any view references in the VM.
What you are trying to do is responsibility of the view as these are things that affect the UI.
In your view you can do:
Remember that MVVM (and ReactiveUI) are about separating responsibilities, the ViewModel defines the business and presentation logic, not view details like control handling.
EDIT: See if this code works for you:
I don't have VS at hand, I hope it works for you.