Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > TreeView > Programmatically select item and focus it

Answered Programmatically select item and focus it

Feed from this thread
  • Predrag avatar

    Posted on Dec 8, 2011 (permalink)

    Hi,

    I’m using RadTreeView with HierarchicalDataTemplate. I need to programmatically select an item in the tree view and expand it.
    I’m using approach described here http://blogs.telerik.com/xamlteam/posts/11-01-12/treeview-bringintoview.aspx. I need also to focus the item in order to be highlighted. When I call item.Focus(); almost every time nothing happens (it returns false). In debug mode I was able to set item.IsFocused = true; and then it worked (item was highlited), but you cannot access to IsFocused setter property of RadTreeViewItem from code because it is protected.

    How can I focus this selected item from code in order to highlight it?

    Best regards,

    Predrag

    Reply

  • Kiril Stanoev Kiril Stanoev avatar

    Posted on Dec 8, 2011 (permalink)

    Hello Predrag,

     Could you please try calling Focus() within a Dispatcher:

    this.Dispatcher.BeginInvoke(() =>
    {
        treeViewItem.Focus();
    });

    Let me know if this helps.

    Best wishes,
    Kiril Stanoev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

    Reply

  • Predrag avatar

    Posted on Dec 9, 2011 (permalink)

    Hello Kiril,

    I tried calling the Focus() with Dispatcher, but it didn’t help.  

    My page is bounded to view model, on it I have refresh button (which uses relative binding to reload command), than I have user control which has RadTreeView and user control for selected item details. When data reload is finished view model reload command will send reloaded message, my Tree view control is registered to this message. What it actually does is reexpand tree view nodes and reselects view model selected item.

    It looks like my UserControl that contains RadTreeView is not focused, because when I click on control selected tree view item becomes highlighted (then also my user control is focused).

    If is not possible to focus on item is there any way to change the style for selected unfocused RadTreeViewItem in RadTreeView with HierarchicalDataTemplate?


    Best regards,
    Predrag

    Reply

  • Answer Petar Mladenov Petar Mladenov admin's avatar

    Posted on Dec 14, 2011 (permalink)

    Hi Predrag,

     Please check out the attached project where I extracted the default ItemContainerStyle of the RadTreeView and changed the Background of the RadTreeViewItems when they are Selected but not focused:

    <LinearGradientBrush x:Key="ControlSubItem_Background_UnFocus" EndPoint="0,1">
                <!--<GradientStop Color="#FFf8f6f9" Offset="0"/>
                <GradientStop Color="#FFf0f0f0" Offset="1"/>-->
                <GradientStop Color="Blue" Offset="0" />
                <GradientStop Color="DeepSkyBlue" Offset="1" />
            </LinearGradientBrush>
    <Border x:Name="SelectionUnfocusedVisual"
                               Grid.Column="2"
                               Grid.ColumnSpan="6"
                               BorderBrush="{StaticResource ControlSubItem_OuterBorder_UnFocus}"
                               BorderThickness="{StaticResource ControlSubItem_OuterBorderThickness}"
                               CornerRadius="{StaticResource ControlSubItem_OuterCornerRadius}"
                               Visibility="Collapsed">
                           <Border Background="{StaticResource ControlSubItem_Background_UnFocus}"
                                   BorderBrush="{StaticResource ControlSubItem_InnerBorder_UnFocus}"
                                   BorderThickness="{StaticResource ControlSubItem_InnerBorderThickness}"
                                   CornerRadius="{StaticResource ControlSubItem_InnerCornerRadius}" />
                       </Border>
     
    Please let us know if this satisfies you or not.  Greetings,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

    Attached files

    Reply

  • Predrag avatar

    Posted on Dec 26, 2011 (permalink)

    Thank you very much. This is exactly what I needed.

    Reply

  • Mark avatar

    Posted on Mar 6, 2012 (permalink)

    Any chance you can get the selected/focused item to scroll into view if it is below display area.

    Shorten the browser window, select the item at the bottom, scroll to the top, click the selector button - where is the selected item? Not in view...

    Reply

  • Hristo Hristo admin's avatar

    Posted on Mar 6, 2012 (permalink)

    Hi,

    You could take a look at the following blog post for about BringIntoView feature of RadTreeView: http://blogs.telerik.com/xamlteam/posts/11-01-12/treeview-bringintoview.aspx 
    The post contains a sample project you could use to check if this approach is applicable to your case.

    Kind regards,
    Hristo
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

    Reply

  • Mark avatar

    Posted on Mar 6, 2012 (permalink)

    Thanks Hristo, that is precisely what I was looking for, who'd of thunk it was right there in the object tree!

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > TreeView > Programmatically select item and focus it
Related resources for "Programmatically select item and focus it"

Silverlight TreeView Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]