Bind ContextMenu to Command

4 posts, 0 answers
  1. Kirk Quinbar
    Kirk Quinbar avatar
    48 posts
    Member since:
    Nov 2007

    Posted 13 Jan 2011 Link to this post

    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
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Jan 2011 Link to this post

    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>>
  3. DevCraft banner
  4. Kirk Quinbar
    Kirk Quinbar avatar
    48 posts
    Member since:
    Nov 2007

    Posted 18 Jan 2011 Link to this post

    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
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 18 Jan 2011 Link to this post

    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>>
Back to Top