KRichardson
KRichardson

Reputation: 1010

wpf RadGridView / RadContextMenu binding issue

I have a RadGridView with a different RadContextMenu's shown, depending on the cell clicked.

I have a list of RadMenuItems as an Observable collection. When i set the following code on the StackPanel that the Grid is located in (parent container) it shows all of the items properly.

When i move it into a stackpanel in an itemtemplate, it will not work. The same code to find the ViewModel works with the button command in a previous item, but not in the context menu.

I have attached to the Opening event in the View and the ItemsSource is null....

Any thoughts?

This button command works properly:

                   <telerik:GridViewColumn Header="View" Width="75">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="View" FontSize="16" Margin="2" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ViewTrips:ViewTripsView}}, Path=DataContext.ViewTripCommand}" CommandParameter="{Binding Path=TripID}"></Button>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>

the context menu will not be populated:

                <telerik:GridViewColumn Name="TripStatus" Header="Status" Width="75" SortMemberPath="TripStatus.Name">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>                                   
                                <telerik:RadContextMenu.ContextMenu>
                                    <telerik:RadContextMenu ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ViewTrips:ViewTripsView}}, Path=DataContext.StatusItems}">

                                    </telerik:RadContextMenu>

                                </telerik:RadContextMenu.ContextMenu>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Path=TripStatus.Name}"></TextBlock>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Path=SalesOrder.PaymentStatusText}"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>

declaration of StatusItems

    private ObservableCollection<RadMenuItem> _StatusItems;

    public ObservableCollection<RadMenuItem> StatusItems
    {
        get
        {
            return _StatusItems;
        }
        set
        {
            _StatusItems = value;
            RaisePropertyChanged("StatusItems");
        }
    }

and the following code works, outside of the grid item cell:

<StackPanel>                                   
  <telerik:RadContextMenu.ContextMenu>
    <telerik:RadContextMenu ItemsSource="{Binding  Path=StatusItems}">                                           
    </telerik:RadContextMenu>                                        
  </telerik:RadContextMenu.ContextMenu>
  ... code for RadGridview....
</StackPanel>

Upvotes: 2

Views: 3939

Answers (1)

KRichardson
KRichardson

Reputation: 1010

After not getting any replies back, I contacted Telerik Support and they fixed the issue. They stated that there is some problem when using FindAncestor in the binding, which is also an issue in a regular context menu. Their solution was to use a static resource for the ViewModel and then assign it as a static resource.

I use the ViewModelLocator from MVVM light, so my View is bound like so: DataContext="{Binding Path=ViewTripsViewModelBinding, Source={StaticResource Locator}}"

I added a CollectionViewSource to the resources, and bound it to my status items. I then bound the context menu to the static resource.

  <Common:WindowBase.Resources>
        <CollectionViewSource x:Key="StatusItems" Source="{Binding Path=StatusItems}"></CollectionViewSource>
    </Common:WindowBase.Resources>

And my context menu:

<telerik:RadContextMenu ItemsSource="{Binding Source={StaticResource StatusItems}}" />

Upvotes: 4

Related Questions