This is a migrated thread and some comments may be shown as answers.

Bind ContextMenu to Command

3 Answers 152 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Kirk Quinbar
Top achievements
Rank 1
Kirk Quinbar asked on 13 Jan 2011, 11:31 PM
using Q3 2010 release of your controls, I have a radtreeview context menu. I would like to bind it to a command property on a viewmodel and pass the currently selected item as a commandproperty.

<telerik1:RadContextMenu.ContextMenu>
    <telerik1:RadContextMenu x:Name="ContextMenu" >
        <telerik1:RadMenuItem Header="Properties" Command="{Binding ContextMenuCommand}" CommandParameter="{Binding ElementName=treeView, Path=SelectedItem}"  />
    </telerik1:RadContextMenu>
</telerik1:RadContextMenu.ContextMenu>

This does not work as shown, meaning the command is called, but the value of the property is always null. i think its because at this level the binding doesnt know anything about the treeview or cant find it in the visual tree? What would be the best way to have a context menu pass the selecteditem to the command? I have also tried binding the SelectedItem at the treeview level to my viewmodel, but that didnt seem to work either, although from what I have read, the Q3 2010 release should support that?

ideas?


Kirk

3 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 18 Jan 2011, 04:30 PM
Hello Kirk Quinbar,

Since the ContextMenu opens in a Popup that is in different visual tree than your "treeView",
the search algorithm which uses  {Binding ElementName=treeview, Path=SelectedItem} cannot find the tree. A possible workaround is to bind the CommandParameter to the SelectedItem property in your ViewModel. The described approach is demonstrated in the attached file. So give it a try and let us know if it works for you.

Best wishes,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Kirk Quinbar
Top achievements
Rank 1
answered on 18 Jan 2011, 05:35 PM
are there certain circumstances where SelectedItem won't pass through to the viewmodel or doesnt work? here is how we have our treeview setup and I can never get SelectedItem to pass through to our viewmodel.

<telerik1:RadTreeView 
    Grid.Row="2" 
    Grid.ColumnSpan="2" 
    x:Name="treeView"
    ExpanderStyle="{StaticResource ExpanderStyle}"
    ItemsSource="{Binding ViewRecipients}"
    SelectionMode="Multiple"
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
    ItemTemplate="{StaticResource RecipientTemplate}"
    IsExpandOnDblClickEnabled="False"    
    IsEditable="False"
    IsVirtualizing="True"
    telerik1:TreeViewPanel.VirtualizationMode="Standard" 
    Margin="0,5" 
    ItemContainerStyle="{StaticResource RecipientsTree}" 
    Style="{StaticResource RadTreeViewStyle1}"
    Loaded="treeView_Loaded"
    MouseRightButtonDown="treeView_MouseRightButtonDown"
    >
    <telerik1:RadContextMenu.ContextMenu>
        <telerik1:RadContextMenu x:Name="ContextMenu" >
            <telerik1:RadMenuItem Header="Properties" Command="{Binding ContextMenuCommand}" CommandParameter="{Binding SelectedItem}" />
        </telerik1:RadContextMenu>
    </telerik1:RadContextMenu.ContextMenu>
</telerik1:RadTreeView>

SelectedItem is a simple dependency property on our viewmodel. we are using heirarchial item templates and a databindings collection since we want to support being able to select more than one item at a time. Just as a test, i did try to change the SelectMode to Single, but that didnt make any difference.

Kirk
0
Petar Mladenov
Telerik team
answered on 18 Jan 2011, 06:53 PM
Hi Kirk Quinbar,

As I manage to understand, the provided sample works, but it doesn`t apply to your xaml ?
When binding a TreeView to a ViewModel , it is important that the ItemSource of the tree is ObservableCollection (which implements INotifyCollectionChanged and INotifyPropertyChanged) or a collection that implements INotifyCollectionChanged. When binding the SelectedItem property, it is crucial for the ViewModel implements INotifyPropertyChanged  and the SelectedItem property of the ViewModel raises properly NotifyPropertyChanged event. You can inspect this in the solution I sent you and compare it with yours. Furthermore, if you still have difiiculties, it would be best if we can view your code and investigate in depth since only xaml is insufficient.
Hope this helped you.

Best wishes,
Petar Mladenov
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
TreeView
Asked by
Kirk Quinbar
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Kirk Quinbar
Top achievements
Rank 1
Share this question
or