0

我有一个列表框,这个列表框包含图像控件。

我想在列表框项的单击事件上更改运行时图像路径。

如何更改我的图像控制路径?

我的代码是这样的:

  <ListBox x:Name="TransactionList" Width="450" Height="450" Margin="0,0,0,0">    
    <ListBox.ItemTemplate >
      <DataTemplate>

            <StackPanel Orientation="Horizontal" Width="450" Height="auto" Margin="0,0,10,0" >
                <StackPanel Orientation="Horizontal"  Width="300" Height="auto" Margin="0,0,1,10">
                    <TextBlock Height="35" Width="250"  Margin="10,0,0,90" Text="{Binding SONGNAME}" FontSize="22" Foreground="Red" Name="tbSubCategories" />
                    <TextBlock Height="25" Margin="-250,10,180,30" Text="SingBy:" FontSize="18" Foreground="Gray" Name="tbsingby" />
                    <TextBlock Height="auto" TextWrapping="Wrap" Width="210" Margin="-180,50,0,70" Text="{Binding  SINGBY}" FontSize="18" Foreground="Gray" Name="tbSingBy" />
                     <TextBlock Height="25"  Margin="-280,60,200,0" Text="MusicBy:" FontSize="18" Foreground="Gray" Name="tbMusicby" />
                     <TextBlock Height="auto" TextWrapping="Wrap" Width="200" Margin="-205,100,0,30" Text="{Binding MUSICBY}" FontSize="18" Foreground="Gray" Name="tbMusicBy" />
                </StackPanel>
            <StackPanel Orientation="Vertical"  Height="auto" Width="auto" Margin="0,50,0,35">
              <Image Name="imgsubcatagorie" Width="40" Height="40"  VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/play1.png" />
            </StackPanel>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox> 

当我单击列表框项时,我想更改 imgsubcatagories 图像控制路径。帮我

4

1 回答 1

0

好吧,这并不容易。

如果可以获取您的图像对象,则可以像这样轻松完成:

imgsubcatagorie.Source = ModifyImageSource(new Uri("yourUri", UriKind.RelativeOrAbsolute));

 private ImageSource ModifyImageSource(Uri uri)
        {
                image = new BitmapImage(uri);
                return image;          
        }

但是,您不能只拿走这个对象。主要问题是如何获得它。

您应该以某种方式从 TransactionList 中提取它。(另外,你的结构很重:listbox=>stackpanel=>stackpanel=>image)

我的第一个想法是:使用循环遍历 TransactionList 中的所有项目并尝试在那里找到图像。


可能的解决方案

我对 ListBox.ItemTemplate 功能并不熟悉,而且我没有你的完整代码,但无论如何......

直接方法:尝试使用循环查找项目。

foreach (var x in TransactionList.Items)
            {
                if (x is Image)
                {
                    image = (Image)x;
                    break;
                }
            }

然后在事件处理程序中使用此图像对象。

查找具体类型的元素。

还要检查这个问题:

在 Listbox.ItemTemplate 中查找控件

不适用于 wp7,但可能有效。

于 2012-12-04T05:19:58.577 回答