我有一個關於綁定到視覺畫筆視覺的問題。如果我在XAML中定義它,它會起作用。如果我以編程方式定義相同的東西,那麼它不起作用。這是一個例子。Wpf可視化畫筆視覺綁定到MediaElement
<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>
我試過以下屬性作爲視覺和媒體元素。
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;
}
}
}
編輯3.我已經調整了綁定錯誤,它被綁定。它出現在Live Visual Tree/Live屬性資源管理器中。它只是不顯示。
有誰知道爲什麼?
在窗口上設置'x:Name =「MainWindow」'。 – Clemens
你說得對。我會更新我的代碼。但視頻仍然沒有出現。 – Haydn