【发布时间】:2014-04-25 16:14:31
【问题描述】:
这是我的场景的一些代码 sn-ps。在 .xaml 方面,我有一个 ItemsControl:
<ItemsControl ItemsSource="{Binding StatList}" ItemTemplate="{StaticResource statBox}"></ItemsControl>
StatList 只是一个对象列表。
DataTemplate 包含此 TextBlock
<TextBlock x:Name="DataTextBlock" Background="Transparent" DockPanel.Dock="Top" HorizontalAlignment="Center"
Foreground="White" FontSize="11">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<iBehaviors:InvokeMethodTrigger Method="UpdateBinding"
Target="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}"
Parameter="{Binding ., RelativeSource={RelativeSource AncestorType=TextBlock}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
这在我第一次在我的 ViewModel 上设置我的 StatList 时起作用,TextBlock 本身作为参数传递给 InvokeMethodTrigger。但是,如果我更改视图模型上的 StatList,则下次发生 Loaded 事件时,触发 InvokeMethodTrigger 时,Parameter 为 null。
奇怪的是,当我将参数绑定更改为:
Parameter="{Binding ElementName=DataTextBlock}"
当我更改我的 StatList 时,它第二次起作用,TextBlock 作为参数传递。我一辈子都想不通为什么!有什么解释吗?
【问题讨论】: