0

我的 xaml 中有以下 ContextMenu:

<ContextMenu ItemsSource="{Binding RSPContextMenuCommands}">
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="CommandParameter" Value="{Binding}" />
                                <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.ShowASPCommand}" />
                                <Style.Triggers>                          
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding SpName}" Value="ASP">
                                        <Setter Property="Header" Value="Show Additional Service Providers" />
                                        <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.TransferCommand}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                        <ContextMenu.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Margin="5,0,0,0" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PhoneNumber, PresentationTraceSources.TraceLevel=High}" />
                                </StackPanel>
                            </DataTemplate>
                        </ContextMenu.ItemTemplate>
                    </ContextMenu>

其中,RSPContextMenuCommands是 Schedule(class) 类型的集合。Schedule 中有PhoneNumber属性。TransferCommand与声明 RSPContextMenuCommands 的级别相同。我得到 ShowASPCommand 和 TransferCommand 但不是PhoneNumber。我尝试了各种 RelativeSource 组合,但没有奏效。什么应该是它的正确RelativeSource。也试过RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PhoneNumber

4

1 回答 1

0

解决方法。我将ItemTemplate移到ItemContainerStyle上方。

<ContextMenu ItemsSource="{Binding RSPContextMenuCommands}">
                        <ContextMenu.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding PhoneNumber}" />
                                </StackPanel>
                            </DataTemplate>
                        </ContextMenu.ItemTemplate>
                        <ContextMenu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.ShowASPCommand, PresentationTraceSources.TraceLevel=High}" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding SpName}" Value="ASP">
                                     <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=DataContext.TransferCommand}" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>

但是,如果任何人都可以为我提供有关RelativeSource方法的适当文档。我真的很感激。:-)

于 2012-10-24T09:02:45.603 回答