I have a question about binding to a visual brush visual. If I define it in XAML it works. If I define the the same thing programmatically then it does not work. Here is an example.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="MyWindow"
Title="MainWindow" Height="350" Width="525">
<Grid >
<Rectangle Height="299" Width="400" Stroke="Red" StrokeThickness="20" >
<Rectangle.Fill>
<VisualBrush TileMode="None" Stretch="UniformToFill" Visual="{Binding ElementName=MyWindow, Path=Stuff}">
<!--<VisualBrush.Visual>
<MediaElement Source="MovingImages_2017-03-10-05-02-22.wmv" LoadedBehavior="Play" />
</VisualBrush.Visual>-->
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
I have tried the following property as a visual and a media element.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public Visual Stuff { get; set; }
public MainWindow()
{
InitializeComponent();
MediaElement me = new MediaElement();
me.Source = new Uri("MovingImages_2017-03-10-05-02-22.wmv", UriKind.Relative);
me.LoadedBehavior = MediaState.Play;
this.Stuff = (Visual) me;
}
}
}
Edit 3. I have turned up binding errors and it is being bound. It turns up in the Live Visual Tree / Live property Explorer. It just does not show up.
Does anyone know why?
It seems like this is because your
Stuffproperty does not notify it changed. You have three choices to solve this:1) Implement the INotifyPropertyChanged interface in your MainWindow class and when setting the
Stuffproperty raise the PropertyChanged event.2) Define the
Stuffproperty as a DependencyProperty which has inherent property change notifications (since your class is a DependencyObject)3) Assign the property value before calling InitializeComponent. This will obviously work only once during initialization.
These are the two available methods for a property to participate in the Binding game