C# 当前上下文中不存在错误

C# 当前上下文中不存在错误,c#,wpf,xaml,windows-phone-7,C#,Wpf,Xaml,Windows Phone 7,当我尝试将XAML中的图像绑定到代码中的bitmapImage对象时,它给了我 “当前上下文中不存在”错误 代码 XAML 首先,确保PhotoSource是一个公共属性,因为WPF不识别任何其他内容 其次,确保正确设置DataContext属性。如果属性是窗口代码隐藏的一部分,则可以通过设置以下行将DataContext设置为窗口: DataContext="{Binding RelativeSource={RelativeSource self}}" 在xaml中的窗口声明中,它看起来像这

当我尝试将XAML中的图像绑定到代码中的bitmapImage对象时,它给了我 “当前上下文中不存在”错误

代码

XAML

首先,确保PhotoSource是一个公共属性,因为WPF不识别任何其他内容

其次,确保正确设置DataContext属性。如果属性是窗口代码隐藏的一部分,则可以通过设置以下行将DataContext设置为窗口:

DataContext="{Binding RelativeSource={RelativeSource self}}"
在xaml中的窗口声明中,它看起来像这样:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource self}}">
  <!-- Your Code here -->
</window>
首先,确保PhotoSource是一个公共属性,因为WPF不识别任何其他内容

其次,确保正确设置DataContext属性。如果属性是窗口代码隐藏的一部分,则可以通过设置以下行将DataContext设置为窗口:

DataContext="{Binding RelativeSource={RelativeSource self}}"
在xaml中的窗口声明中,它看起来像这样:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource self}}">
  <!-- Your Code here -->
</window>
您需要:

创建要绑定到应用程序的类。例如:

private class Photo {
    public string PhotoSource {get; set;}
}
创建要绑定的集合。例如,列表照片=新列表

在列表中添加一些数据。例如,Photos.addnewphoto{PhotoSource=yourbistmap}; 将它绑定到您的列表中。PhotoList.ItemsSource=照片; 您需要:

创建要绑定到应用程序的类。例如:

private class Photo {
    public string PhotoSource {get; set;}
}
创建要绑定的集合。例如,列表照片=新列表

在列表中添加一些数据。例如,Photos.addnewphoto{PhotoSource=yourbistmap}; 将它绑定到您的列表中。PhotoList.ItemsSource=照片;
您需要创建这样的属性。错误是自解释的-PhotoSource不存在。是,但它确实存在:@LivingThing:该代码绑定到不存在的属性。您需要创建这样的属性。错误是自解释的-PhotoSource不存在。是,但它确实存在:@LivingThing:该代码绑定到不存在的属性。