2

这种绑定工作得很好

<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 实际将哪个祖先视为模板化父级?

ps 以下绑定工作正常

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

4

1 回答 1

1

只有当我们为 Parent 定义 ControlTemplate 时,才能使用 TemplatedParent 绑定。你没有这样做。在这里获得更多。

于 2013-11-08T05:09:42.897 回答