Link MediaElement from VM to View - wpf

My situation is something like this:
I have a class called MediaElementPlayer which represents a video player, and acts as a ViewModel for a DataTemplate. It is based on this idea with adjustments to WPF and MVVM light instead of Silverlight.
It works great, but I need to be able to maximize the video too.
For that I created a different window called FullScreenWindow, which holds a single ItemContainer to draw the video player. When I create this window, I pass it the MediaElementPlayer object from the main window. This works too.
The problem is that when the full screen window is opened, the media's position is not retained, that's because it creates a new MediaElement.
Is there a way I can take the MediaElement object I already have in mt MediaElementPlayer(VM) and draw it on the view, instead of creating a new one? That will help me retain media position across windows.
Thank you very much in advance.

You can create a new MediaElement UserControl in your FullScreenWindow then use Binding to set its size: you can bind the MediaElement object actual Size to a property of the ViewModel MediaElementPlayer (you can update these values by creating a behaviour attached to SizeChanged event of the UserControl).
Then, the ViewModel of the FullScreenWindow can set the Size of the its MediaElement basing on the property of the ViewModel.

Related

Drawing Control Visual Elements onto a Rectangle

I've got a custom composite WPF control (AvalonEdit) in my application that I'd like to animate whenever its Text property is changed. What I intend to do is:
Create a copy of the control's visual representation before the text is changed and paint it over a rectangle.
Fade out the above rectangle, update the text property and fade in the control using the DoubleAnimation and Storyboard classes.
I've got #2 figured out but haven't got a clue about how I'd achieve #1. Any help would be appreciated.
For (1) there are a couple of approaches that spring to mind:
VisualBrush - A visual brush is a brush which is defined by a complex UI element. In other words, you can create a visual tree of elements and use this to create your brush. See the tutorial here. I think in your case you would have to define your UI twice, i.e. have an instance of your AvalonEdit control as the 'visual' for you VisualBrush, so perhaps not ideal
WriteableBitmap - A writeable bitmap allows you to copy part of your UI into a bitmap where you can manipulate he pixel data. Whilst you do not need pixel-level manipulation it is still a convenient mechanism for cloning your UI. See this tutorial I wrote here.

Assign WritableBitmap to an ImageBrush.ImageSource property

I want to have an animated panorama control background in Windows Phone application.
I have an algorithm that constantly draws onto the WritableBitmap the desired image.
I have bound the Panoramas' background property to ViewModels' BackgroundImageBrush property.
Then at construction time of my ViewModel I assign
BackgroundImageBrush = new ImageBrush {ImageSource = _outputWriteableBitmap};
I get no Binding errors in output and when I check the binding at with the debugger, I get the correct linkage of BackgroundImageBrush.ImageSource to a WritableBitmap.
Every time I redraw the _outputWriteableBitmap I do call Invalidate.
Still my panorama background is blank!
What am I doing wrong in theory? Is it even possible to have an animated background in Windows Phone's Panorama?
Thanks.
The first step here is to make sure that the binding is actually working. Try creating a static image brush and binding your panorama background to that. If that works, then the issue is with the way you're updating the image--either the image isn't rendering properly or you're not calling PropertyChanged correctly.
I've bound a panorama background image to a property in a viewmodel before, so it is doable.
Incidentally, how often will you be updating this image? It could be brutal on your performance if it happens frequently.
A way offered in the comments below the question has the potential answer.
This blog post shows how to at least change the panorama background at runtime, tough I didn't manage get it to play animation generated at runtime.

Cache ContentControl/ContentPresenter Bound to ViewModel

If I have a ContentControl/ContentPresenter whose content will be set to a ViewModel - and I have a type-referential data template for the ViewModel - is there a clean, MVVM-compliant way to take a "snapshot" of the ContentControl/ContentPresenter when it has rendered everything?
The idea is that I will have up to three or four ViewModels "open" at any given point in time, and I will have a ListBox bound to a collection of the ViewModels. There will be one ContentControl/ContentPresenter displaying the "current" view model being viewed. If the user moves their mouse over one of the ViewModels in the ListBox, I want to display a scaled down preview of the ViewModel for them. Rather than rendering the content everytime, I want to cache the content for a ViewModel once it has been displayed initially in the main ContentPresenter.
Has anyone seen anything like this before?
Chris
You can create a bitmap of any visual using RenderTargetBitmap. The bitmap will include the entire visual tree of the visual.
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
http://msdn.microsoft.com/en-us/library/aa969775.aspx

WPF element binding across separate windows

I can do element-to-element binding in WPF: For example, I've got a window that has a slider control and a textbox, and the textbox dynamically displays the Value property of the slider as the user moves the slider.
But how do i do this across separate windows (in the same project, same namespace)?
The reason is that my main application window containing the textbox has a menu option that will open an 'options' window containing the slider control.
You should use a (global) ViewModel, containing the data you need to share, and bind to the property from that ViewModel.
This way the changes in either of windows are reflected in the bound data object, and back.
You dont. Point. Databinding has to go to an element accessible in the same control.
What you can do is have the options menu bind go an object that it has in it's own code (property) that gets populated to the same object the othe rwindow uses as data source.

Forcing a repaint of a WPF control

I currently try to create classes for a paint-like WPF application. I have to base classes LineMovement (line from StartPoint to EndPoint) and PathMovement (line going through all points specified in a property Points of type PointCollection). These classes inherit from Control and get their looks through a ControlTemplate.
The ControlTemplate also adds an Adorner to the AdornerLayer of the Movement objects containing a little visual marker for every moveable point of the specific line. These markers support dragging with a mouse.
The problem I have is that somehow my Movement classes don't repaint when their points are moved. I debugged my code with Mole and found out that the Polyline used to visualize the line gets the changed point values (visible in its Points property) but it just doesn't repaint.
How can I force a repaint of a WPF control?
TemplateBinding doesn't support two-way data binding (i.e. updating the Points collection with the new values of the Polyline). It's meant only for one-way data binding for use in control templates. See Bea Stollnitz's blog entry: http://bea.stollnitz.com/blog/?p=38
Turns out that TemplateBinding is pure evil.
When I bind the Points of the Polyline by {TemplateBinding Points} it doesn't update itself, whereas when I bind it with {Binding RelativeSource={RelativeSource TemplatedParent}} it works perfectly.
Note to myself: Never use this goddamn TemplateBinding again.
You need to make your Movement objects' DPs have the AffectsArrange metadata property (http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadataoptions.aspx) - that way when the property changes, WPF knows it should redraw

Resources