本文介绍了LongListSelector项模板中的Image Control上的ChangePropertyAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长列表选择器,我有一个datatemplate作为项目模板,包含一个图像。我希望源根据模型中的属性进行更改。我尝试使用转换器,但我无法让它工作。

I have a long list selector and i have a datatemplate as item template, containing an image. I want the source to change based on a property from the model. I tried with a converter but i could't get it to work.

现在我正在尝试使用触发器。我有:

Now i'm trying with triggers. I have:

<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>

除了图像重复之外,它几乎是我想要的。因为在电影中有另一部电影的图片。我认为这是因为我按元素名称绑定,图像控件有多个实例(每个项目一个),但我认为他们无法看到对方。任何帮助高度赞赏。

It work almost how i want it to, except that images repeat themselves. As in a movie has the picture of another movie. I think it's because i bind by element name and the image control has multiple instances (one for each item), but i would think they can't see each other. Any help highly appreciated.

编辑:

经过进一步调查后,似乎发生这种情况是因为长列表选择器。

After further investigation, it seems that this happens because of the long list selector.

我首先加载40个项目,然后加载另外40个项目,但第二批40个项目从第一批获取图片。如果我提出了属性更改事件,那么第二批中的图片将在所有重复的项目上设置。我不知道为什么会发生这种情况。

I first load 40 items, and then load another 40, but the second batch of 40 items get the pictures from the first batch. If i raise a property changed event, then the pictures from the second batch are set on all items repeating themselves. I have no idea why this is happening.

如果我再次在IsCategoryCurrent上加载另外40个并且提升属性,则第3批中的图片将被设置3次。

If i load another 40 and raise property changed on IsCategoryCurrent again, the pictures from the 3rd batch get set 3 times.

推荐答案

我设法解决了这个问题:

I managed to fix it:

<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的属性更改事件。

And i raise a property changed event of IsCategoryCurrent at every change.

这篇关于LongListSelector项模板中的Image Control上的ChangePropertyAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 08:21