我將圖像Source
綁定到我的代碼中的BitmapImage
,但它不顯示。圖像源綁定不起作用
XAML:
<Window x:Class="bleh.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="600" d:DesignWidth="600">
<Grid x:Name="LayoutRoot">
<Image x:Name="current" HorizontalAlignment="Center" Stretch="None" VerticalAlignment="Center" Source="{Binding Picture}" />
</Grid>
</Window>
和我的CS:
public partial class MainWindow : Window, INotifyPropertyChanged
{
/// <summary>
/// Event implementing INotifyPropertyChanged interface.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public BitmapImage Picture { get; set; }
public MainWindow()
{
Uri uri = new Uri("Images/xpto.jpg", UriKind.Relative);
this.Picture = new BitmapImage(uri);
InitializeComponent();
//setup();
}
}
奇怪的是,窗口與圖像的大小打開,但我看不到圖像。我也嘗試在xaml中手動分配,並且它可以工作。
做current.source="Images/xpto.jpg"
也有效。
工程,你是如何做到這一點在XAML?我在窗口標籤上試過'DataContext =「MainWindow」'並且看起來不起作用 – GriffinHeart 2012-07-19 00:17:15
爲你的窗口命名(例如x:Name =「Main」),然後使用Source =「{Binding Picture,ElementName = Main}「 – lesscode 2012-07-19 00:23:32
請注意,儘管您可以綁定Xaml中窗口的DataContext(或其中的任何元素),但您必須綁定到視圖中已有的東西(因爲當然,您沒有DataContext ...) – lesscode 2012-07-19 00:29:30