在Contextmenu上找不到RelativeSource属性

时间:2012-10-23 15:55:49

标签: xaml contextmenu relativesource

我在我的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(类)类型的集合。 Schedule中包含 PhoneNumber 属性。 TransferCommand 与声明RSPContextMenuCommands的级别相同。我得到ShowASPCommand和TransferCommand但不是 PhoneNumber 。我尝试了各种RelativeSource组合,但它没有用。什么应该是适当的RelativeSource。还尝试了RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PhoneNumber

1 个答案:

答案 0 :(得分: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 方法提供适当的文档。我真的很感激。 : - )