Question

I'm using a grid control, derived from a Telerik Grid to add a DependencyProperty CurrentSelection so I can use it in Mvvm way.

 public static readonly DependencyProperty CurrentSelectionProperty = DependencyProperty.Register(
        "CurrentSelection", 
        typeof(ObservableCollection<object>), 
        typeof(TelerikGrid),
        new PropertyMetadata(new ObservableCollection<object>(), **DependencyPropertyDebugger**));

It seems to pretty work well every other places I used it...

Now we added a ContextMenu in our grid, right click and print selection, pretty simple... It still seems to work fine.

 <MenuItem Command="{Binding CommandPrintReport}">
                </MenuItem>

This MenuItem has now been Templated to add a "Parameter" button used to open a window dialog.

<MenuItem.HeaderTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <TextBlock DockPanel.Dock="Left" Text="Print selection" Margin="0,0,15,0"/>

                                    <Button Style="{StaticResource MenuItemButton}" DockPanel.Dock="Right" Command="{x:Static NS:ViewModel.CommandReportOptions}">
                                    <Image Source="{Binding Source={x:Static ImagePath:General.ConfigLite}}" />
                                </Button>
                            </DockPanel>
                        </DataTemplate>
                    </MenuItem.HeaderTemplate>

When clicking on this button instead to print the selection, the dependency property seems to be set to null, so we are losing the selection. When looking the the property changed callback, with DependencyPropertyDebugger, I can see the value is set to some selection, but it's not triggered when it set back to null...

private static void DependencyPropertyDebugger(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Console.WriteLine("{0}.{1}: {2}", d.DependencyObjectType.Name, e.Property.Name, e.NewValue);
    }

My commands

public ICommand CommandPrintReport { get; private set; }
    public static ICommand CommandReportOptions { get; set; }

I would appreciate some help Thanks

Was it helpful?

Solution

Instead of using a static command,

Command="{x:Static NS:ViewModel.CommandReportOptions}"

I found a way to not use it static this way

Command="{Binding DataContext.CommandReportOptions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyPage}}

And then it fixed my problem, my current selection keeps its value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top