【问题标题】:Does not exist in the current context error当前上下文中不存在错误
【发布时间】:2013-11-24 13:57:44
【问题描述】:

当我尝试将 XAML 中的图像绑定到背后代码中的 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 不存在。
  • 是的,但确实如此:
  • @LivingThing:该代码绑定到一个不存在的属性。

标签: c# wpf xaml windows-phone-7


【解决方案1】:

你需要:

  • 创建一些你想绑定到你的类。例如:

    private class Photo {
        public string PhotoSource {get; set;}
    }
    
  • 创建要绑定的集合。例如,List&lt;Photo&gt; Photos = new List&lt;Photo&gt;();

  • 将一些数据添加到您的列表中。例如,Photos.Add(new Photo { PhotoSource = yourBitmap });
  • 将其绑定到您的列表。 PhotoList.ItemsSource = Photos;

【讨论】:

    【解决方案2】:

    首先,确保 PhotoSourcepublic 属性,因为 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>
    

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2010-12-17
      相关资源
      最近更新 更多