【问题标题】:How to add image inside stack panel which is inside many xaml elements using C# code如何使用 C# 代码在许多 xaml 元素内的堆栈面板中添加图像
【发布时间】:2014-06-14 12:26:49
【问题描述】:

我有一个包含多个全景项目的 xaml 页面,其中一个全景代码如下

 <phone:PanoramaItem Header="onepan">

      <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
      <StackPanel>
      <ListBox x:Name="PhoneList" Height="486" Background="{x:Null}">
     <ListBox.ItemTemplate>
      <DataTemplate>

       <StackPanel Orientation="Horizontal" Height="108"  >
     <Image Height="100" Margin="5" Stretch="Fill" Width="100" Source="/Assets/ApplicationIcon.png" ></Image>
     <Grid x:Name="ContentPanel" Margin="20,28,0,0"  Width="265" >
      <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
   <RowDefinition Height="Auto"/>
   <RowDefinition Height="Auto"/>
  </Grid.RowDefinitions>
 <TextBlock x:Name="titles" FontSize="30" FontWeight="Bold" TextWrapping="Wrap" LineHeight=" 24" MaxHeight=" 48" LineStackingStrategy="BlockLineHeight" Grid.Row="0" Foreground="Black" FontStyle="Normal" Text="{Binding title}" 
                                            Margin="0,0,0,0"  Tag="{Binding title}" Tap="navigateto"/>
  <TextBlock Grid.Row="2" VerticalAlignment="Top" TextWrapping="Wrap"  Margin="123,-3,0,0" Foreground="Black" FontStyle="Normal" Text="{Binding Date}" Height="27" />
    <TextBlock HorizontalAlignment="Left" Margin="0,-4,0,3" Grid.Row="2" TextWrapping="Wrap"  Text="date :" Width="118" Foreground="Black"/>
   <StackPanel x:Name="ivnod" Orientation="Horizontal" HorizontalAlignment="Right" Height="28" Margin="0,21,10,-33" Grid.Row="2" VerticalAlignment="Top" Width="255">
   <TextBlock TextWrapping="Wrap" Text="Rating" Width="64" Foreground="Black"/>
   //I want to add here a image tag by c# code
    </StackPanel>
   </Grid>
        </StackPanel>
        </DataTemplate>
       </ListBox.ItemTemplate> 
     </ListBox>
       </StackPanel>
      </Grid>

 </phone:PanoramaItem>

我想使用 for 循环在名为 'ivnod' 的堆栈面板中添加图像标签,我可以直接添加图像,但我不知道是否应该在指定堆栈面板之前指定所有这些元素,并且在使用此代码时会显示错误

  Image img = new Image();
  img.Source = new BitmapImage(new Uri("/Assets/ApplicationIcon.png", UriKind.Relative));
  ivnod.Children.Add(img);//-->here ivnod displayed as "not available in current context"

【问题讨论】:

  • 图片路径正确

标签: c# xaml visual-studio-2012 windows-phone-8


【解决方案1】:

你应该把Image控制在那里,你可以将binding传递给它,就像你给TextBlock一样。您只需在 Binding 中将 Source 传递给 Image。这将完美地工作。

<StackPanel x:Name="ivnod" Orientation="Horizontal" HorizontalAlignment="Right" Height="28" Margin="0,21,10,-33" Grid.Row="2" VerticalAlignment="Top" Width="255">
     <TextBlock TextWrapping="Wrap" Text="Rating" Width="64" Foreground="Black"/>
     <Image Source="{Binding imgSource}"></Image>
</StackPanel>

【讨论】:

  • 但是由于图像标签的数量,我将如何使用名称调用图像
  • 当你传递 datacontext 或 itemsource 时,你需要再添加一个属性来绑定该图像控件,这样你就不需要通过调用它的名称来设置图像。它将从绑定属性中获取
  • 这里有一个链接可以帮助您了解数据绑定msdn.microsoft.com/en-us/magazine/hh852595.aspx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多