Stas BZ
Stas BZ

Reputation: 1192

MenuItem + CommandParameter = null ! Why?

I trying to pass a parameter SelectedItems to command, but parameter is always null.

<sd:SharpTreeView Name="MyTreeView" Margin="10,38,120,10" Root="{Binding Tests}" ShowAlternation="True" SelectionMode="Multiple">
    <sd:SharpTreeView.ContextMenu>
        <ContextMenu>
            <MenuItem
                Header="Copy"
                CommandParameter="{Binding ElementName=MyTreeView, Path=SelectedItems}"
                Command="{Binding CopySelectedTests}" />
        </ContextMenu>
    </sd:SharpTreeView.ContextMenu>
        <ListView.View>
...

I new in WPF, so for me it is was not helpful to read answers from here and from others questions. It always remain null.

I have tried it not in ContextMenu and it works fine, but it is not what I want.

EDIT 1

I tried this:

CommandParameter="{Binding Path=UIElement.(sd:SharpTreeView.SelectedItems), RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"

this:

CommandParameter="{Binding Path=SelectedItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}"

and many other, it is always null. It only does not null for this:

CommandParameter="{Binding}"

But it is not what I need.

Upvotes: 1

Views: 1451

Answers (1)

Stas BZ
Stas BZ

Reputation: 1192

I finally solved it:

CommandParameter="{Binding
    Path=PlacementTarget.SelectedItems,
    RelativeSource=
        {RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}
   }"

The important part was to specify PlacementTarget.

Upvotes: 3

Related Questions