Question

I have following ContextMenu in my xaml:

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

where, RSPContextMenuCommands is collection of type Schedule(class). Schedule has PhoneNumber property in it.TransferCommand is at the same level where RSPContextMenuCommands is declared. I am getting ShowASPCommand and TransferCommand but NOT PhoneNumber. I tried various RelativeSource combinations but it didn't work. what should be the proper RelativeSource for it. also tried RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PhoneNumber

Était-ce utile?

La solution

workaround. I moved ItemTemplate above 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>

But still if anyboday can provide me proper documentation for RelativeSource approach. I would really appreciate it. :-)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top