[英]Does not exist in the current context error
当我尝试将我的XAML中的Image绑定到后面的代码中的bitmapImage对象时,它给出了'当前上下文中不存在'错误。
码
BitmapImage bitmapImage = new BitmapImage();
PhotoSource.Source = bitmapImage;
ObservableCollection<BitmapImage> Photos = new ObservableCollection<BitmapImage>();
PhotoList.ItemsSource = Photos;
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,5,12,-10">
<ProgressBar x:Name="progressBar" HorizontalAlignment="Left" Height="40" Margin="0,0,0,0" VerticalAlignment="Top" Width="436" Visibility="Collapsed" IsIndeterminate="True"/>
<ListBox x:Name="PhotoList"
toolkit:TiltEffect.IsTiltEnabled="True"
SelectionChanged="PhotoList_SelectionChange"
HorizontalAlignment="Left" Height="500" Margin="0,40,0,0" VerticalAlignment="Top" Width="450">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel
HorizontalAlignment="Left"
Margin="0,0,0,0"
VerticalAlignment="Top"
/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<StackPanel Orientation="Vertical">
**<Image delay:LowProfileImageLoader.UriSource="{Binding PhotoSource}" Width="99" Height="80"/>**
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
首先,确保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;} }
创建要绑定的集合。 例如, List<Photo> Photos = new List<Photo>();
Photos.Add(new Photo { PhotoSource = yourBitmap });
PhotoList.ItemsSource = Photos;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.