nikotromus
nikotromus

Reputation: 1054

How to control a MediaElement inside of a VisualBrush

I have a MediaElement inside of a VisualBrush object so that I can run multiple video displays while only using one MediaElement. The problem is that I cannot figure out how to control the MediaElement from the code behind. The code behind doesn't recognize the name 'myMedia'.

How do I access this element?

 <Window.Resources>
        <VisualBrush x:Key="Media" Stretch="Uniform">
            <VisualBrush.Visual>
                <MediaElement Name="myMedia" Source="c:\a.mp4" Width="100" Height="100"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Window.Resources>

Upvotes: 0

Views: 111

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18310

You can get resources by key from an element in code behind via the FrameworkElement.Resources property. Then it is just a matter of casting the returned resource to a VisualBrush and accessing and casting its VisualBrush.Visual property.

VB.NET:

Dim VBrush As VisualBrush = TryCast(Me.Resources("Media"), VisualBrush)

If VBrush IsNot Nothing Then
    Dim Media As MediaElement = TryCast(VBrush.Visual, MediaElement)

    If Media IsNot Nothing Then
        'Do your stuff here...
    End If
End If

C#:

VisualBrush VBrush = this.Resources["Media"] as VisualBrush;

if(VBrush != null) {
    MediaElement media = VBrush.Visual as MediaElement;

    if(media != null) {
        //Do your stuff here...
    }
}

Upvotes: 2

Related Questions