我有一个很长的列表选择器,并且有一个数据模板作为项目模板,其中包含图像。我希望源根据模型的属性进行更改。我尝试使用转换器,但无法让它工作。
现在我正在尝试使用触发器。我有:
<Image Name="MovieThumbnail">
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding DataContext.IsCategoryCurrent,ElementName=LayoutRoot}" Value="true">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=MovieThumbnail}" TargetName="Source" Value="{Binding Path=Image120x170}" PropertyName="Source"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding DataContext.IsCategoryCurrent,ElementName=LayoutRoot}" Value="false">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=MovieThumbnail}" TargetName="Source" Value="{x:Null}" PropertyName="Source"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
</Image>
它几乎按照我想要的方式工作,除了图像会重复自己。就像一部电影里有另一部电影的画面一样。我认为这是因为我通过元素名称绑定(bind)并且图像控件有多个实例(每个项目一个),但我认为它们无法看到彼此。非常感谢任何帮助。
编辑:
经过进一步调查,似乎是由于长列表选择器而发生这种情况。
我首先加载 40 个项目,然后加载另外 40 个项目,但是第二批 40 个项目获取了第一批的图片。如果我引发属性更改事件,则第二批中的图片将设置在所有重复的项目上。我不知道为什么会发生这种情况。
如果我加载另外 40 个并再次在 IsCategoryCurrent 上更改属性,则第三批中的图片将设置 3 次。
最佳答案
我设法修复了它:
<Image
Grid.RowSpan="2"
Name="MovieThumbnail"
Stretch="Fill"
Width="130" Height="195"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding DataContext.IsCategoryCurrent,ElementName=LayoutRoot}"
Value="true">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=MovieThumbnail}"
TargetName="Source"
PropertyName="Source">
<ei:ChangePropertyAction.Value>
<BitmapImage CreateOptions="BackgroundCreation"
UriSource="{Binding Path=Image120x170}"/>
</ei:ChangePropertyAction.Value>
</ei:ChangePropertyAction>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding DataContext.IsCategoryCurrent,ElementName=LayoutRoot}"
Value="false">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=MovieThumbnail}"
TargetName="Source"
Value="{x:Null}"
PropertyName="Source"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
</Image>
每次更改时,我都会引发 IsCategoryCurrent 的属性更改事件。
关于image - LongListSelector 项模板中图像控件的 ChangePropertyAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332835/