此绑定工作正常
<ItemsControl ItemsSource="{Binding Tariffs}" Margin="6">
<ItemsControl.ItemTemplate>
<DataTemplate>
<custControls:RoundButton Name="TariffButton" Margin="3"
Content="{Binding TariffName}" Style="{DynamicResource TariffButton}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="PickUpTariff">
<cal:Parameter Value="{Binding Path=Content,
RelativeSource={RelativeSource AncestorType={x:Type custControls:RoundButton}}}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</custControls:RoundButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
此绑定正确传递内容对象。 当我改变对此的绑定时:
--all the same and this line is changed to this
<cal:Parameter Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TariffName}" />
它不起作用! 所以我有一个问题,为什么它不起作用以及如何确定哪个祖先实际上被WPF视为模板化父级?
P.S。以下绑定工作正常
<ItemsControl ItemsSource="{Binding Tariffs}" Margin="6">
更新1。 这是带有模板化父绑定的示例,它可以正常工作。
<ListBox Grid.Row="2" x:Name="StationsListView"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
BorderThickness="0"
DataContext="{StaticResource ViewModelKey}"
SelectionMode="Extended"
ItemsSource="{Binding Stations}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<customControls:VirtualizingWrapPanel x:Name="StationsPanel" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<customControls:RoundButton MinWidth="380" Margin="3" Padding="16"
Style="{DynamicResource CommonButtonStyle}">
<TextBlock Text="{Binding FullName}"
Style="{DynamicResource Verdana22BoldWhite}"
TextTrimming="CharacterEllipsis" />
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="StationHasChosen">
<cal:Parameter
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FullName}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</customControls:RoundButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
RoundButton和VirtualizingWrapPanel都是自定义控件,但仅适用于RoundButton我已明确定义了ControlTemplate。