0

我想在列表中的两个图像之间切换并根据某些条件更新它们的源。但是当第二个图像更新时,我希望第一个保留最后显示的图像。基本上这里发生的事情是两者都得到更新,因为源绑定到两者。如何以有效的方式使其正确?

更新:我渲染一个 D3DImage 并想要覆盖两个图像(如图表轴(backImage)和点(targetImage))。根据条件,我更改轴或点图像。

基本上我所拥有的是:

List<Image> imageList = new List<Image>();
imageList.Add(backImage);
imageList.Add(targetImage);

if(condition)
   imageList[0].Source = someSource;
else
   imageList[1].Source = someSource;
4

2 回答 2

0

你不能完全按照你的要求做......你不能更新Image.Source属性并且仍然Image显示旧图像。但是,您可以使用 XAML 和eachBindingImage.Source属性轻松地做到这一点Image

在实现INotifyPropertyChanged接口的视图模型或代码中添加两个属性:

// Don't copy this - implement the `INotifyPropertyChanged` interface properly
public ImageSource ImageSource1 { get; set; } 
public ImageSource ImageSource2 { get; set; }

设置这些属性:

ImageSource1 = ImageSource2 = someSource;

现在Bind将这些属性添加到Image控件中:

<Image Source="{Binding ImageSource1}" />
<Image Source="{Binding ImageSource12}" />

现在您可以将它们分别设置为相同的图像或不同的图像:

ImageSource2 = anotherSource;
于 2013-10-25T13:39:47.577 回答
0

将 BitmapImage 添加到您的资源中,如下所示:

   <BitmapImage x:Key="Connected" UriSource="sample.png" />

并更改您现有的代码以引用 image :

<Image>
    <Image.Style>
      <Style>
    <Style.Triggers>
      <DataTrigger Binding="{Binding Source={x:Static my:Object.Instance}, Path=Connected, Mode=OneWay}"
                   Value="True">
        <Setter Property="Source"
                Value="{StaticResource Connected}"/>
      </DataTrigger>

    </Style.Triggers>
  </Style>
</Image.Style>
于 2013-10-25T13:22:47.223 回答