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

TreeView SelectedItem doesn't work

2 Answers 723 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ristogod
Top achievements
Rank 2
Ristogod asked on 08 Oct 2010, 08:23 PM

Hi, I am building an application where I need to represent some hierarchical data in a tree. The standard WPF tree fell short on many fronts. So I am doing a trial of your WPF controls.

 

One of the big issues with the standard tree was that it was not (easily) possible to select an item within the tree from outside the tree. I was looking at your documentation, and it clearly shows that you can indeed use the SelectedItem property on your RadTreeView to set the selected item in the tree.

 

So I have implemented your tree and am trying to use that property to set the item, but I cannot get it to work. I have tried both through declarative binding and by setting the property programmatically, and in either case the RadTreeView does not set the correct item on the tree. It simply does nothing.

 

What am I doing wrong? If I can get that feature working with your RadTreeView, along with proper node Double-clicking, we will be purchasing at least developer licenses of the WPF RadControls.

Here is the TreeView code I am doing declaratively:

<t:RadTreeView Grid.Column="0" Grid.Row="2"
        MinHeight="100" MinWidth="100"
        DataContext="{Binding Source={x:Static SizingApp:ProjectManager.Instance}}"
        SelectedItem="{Binding CurrentSelected}">
    <t:RadTreeView.Resources>
        <Style TargetType="{x:Type t:RadTreeViewItem}">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
        <HierarchicalDataTemplate x:Key="LoadTemplate">
            <Grid>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_InputGestureText}"
                                      Click="MenuItem_EditLoadClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Edit_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Click="MenuItem_SaveLoadClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Save_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=load_ContextMenu_SaveAs_Header}"
                                      Click="MenuItem_SaveAsLoadClick" />
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Delete_InputGestureText}"
                                      Click="MenuItem_RemoveClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Delete_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_Header}"
                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=load_ContextMenu_Copy_InputGestureText}"
                                    Click="MenuItem_CopyLoadClick" />
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="StepTemplate"
                ItemsSource="{Binding Loads}"
                ItemTemplate="{StaticResource LoadTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Edit_InputGestureText}"
                                      Click="MenuItem_EditStepClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Edit_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Click="MenuItem_SaveStepClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Save_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_SaveAs_Header}"
                                      Click="MenuItem_SaveAsStepClick" />
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Delete_InputGestureText}"
                                      Click="MenuItem_RemoveClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Delete_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Copy_Header}"
                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Copy_InputGestureText}"
                                    Click="MenuItem_CopyStepClick"/>
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Paste_InputGestureText}"
                                    IsEnabled="{Binding IsLoadCopied, Source={x:Static SizingApp:ProjectManager.Instance}}"
                                    Click="MenuItem_PasteLoadClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding CurrentCopiedLoad,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Paste_Header},
                                            Converter={StaticResource NameConverter},
                                            Source={x:Static SizingApp:ProjectManager.Instance},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Import_Load_Header}"
                                    Click="MenuItem_OpenLoadClick"/>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=step_ContextMenu_Add_Load_Header}"
                                    ItemsSource="{StaticResource mnuLoadSubmenuCommon}" MenuItem.Click="StepMenuItem_AddLoadClick"/>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </Grid>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="ProjectTemplate"
                ItemsSource="{Binding Steps}"
                ItemTemplate="{StaticResource StepTemplate}">
            <Grid>
                <TextBlock Text="{Binding Name}">
                    <TextBlock.ContextMenu>
                        <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources,Key=project_ContextMenu_Edit_InputGestureText}"
                                      Click="MenuItem_EditProjectClick"
                                      DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Edit_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Click="MenuItem_SaveProjectClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Save_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_SaveAs_Header}"
                                      Click="MenuItem_SaveAsProjectClick"/>
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Delete_InputGestureText}"
                                      Click="MenuItem_RemoveClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding Path=Name,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Delete_Header},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Copy_Header}"
                                    InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Copy_InputGestureText}"
                                    Click="MenuItem_CopyProjectClick" />
                            <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Paste_InputGestureText}"
                                    IsEnabled="{Binding IsStepCopied, Source={x:Static SizingApp:ProjectManager.Instance}}"
                                    Click="MenuItem_PasteStepClick">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding CurrentCopiedStep,
                                            StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=project_ContextMenu_Paste_Header},
                                            Converter={StaticResource NameConverter},
                                            Source={x:Static SizingApp:ProjectManager.Instance},
                                            Mode=OneWay}" />
                                </MenuItem.Header>
                            </MenuItem>
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_ImportStep_Header}"
                                    Click="MenuItem_OpenStepClick" />
                            <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Project_AddStep_Header}"
                                    Click="MenuItem_AddStepsClick"/>
                        </ContextMenu>
                    </TextBlock.ContextMenu>
                </TextBlock>
            </Grid>
        </HierarchicalDataTemplate>
    </t:RadTreeView.Resources>
    <t:RadTreeViewItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Header}"
            ItemsSource="{Binding Projects}"
            IsExpanded="True"
            ItemTemplate="{StaticResource ProjectTemplate}">
        <t:RadTreeViewItem.ContextMenu>
            <ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
                <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_NewProject_Header}"
                        Icon="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=addProject_Image_Small}"
                        Click="NewProject_Click"/>
                <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_OpenProject_Header}"
                        Icon="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=openProject_Image_Small}"
                        Click="OpenProject_Click"/>
                <MenuItem Header="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_SaveAll_Header}"
                        InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_SaveAll_InputGestureText}"
                        Click="SaveAllProjects_Click"/>
                <MenuItem InputGestureText="{Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_InputGestureText}"
                        IsEnabled="{Binding IsProjectCopied, Source={x:Static SizingApp:ProjectManager.Instance}}"
                        Click="MenuItem_PasteProjectClick">
                    <MenuItem.Header>
                        <TextBlock Text="{Binding CurrentCopiedProject,
                                StringFormat={Resx ResxName=PSSPECApplication.Controls.ProjectControlResources, Key=projectTree_Paste_Header},
                                Converter={StaticResource NameConverter},
                                Source={x:Static SizingApp:ProjectManager.Instance},
                                Mode=OneWay}" />
                    </MenuItem.Header>
                </MenuItem>
            </ContextMenu>
        </t:RadTreeViewItem.ContextMenu>
    </t:RadTreeViewItem>
</t:RadTreeView>

Now the Projects property that the first node ItemsSource is bound to works just fine. However, the SelectedItem property on the TreeView itself seems to do nothing. Both the Projects property and the CurrentSelected property are both DependencyProperties. Both have call back event handlers that fire off INotifyPropertyChanged PropertyChanged event handlers indicating the properties have changed. I have other controls on the application that are bound to CurrentSelected and they change along with CurrentSelected just fine.

So why doesn't the RadTreeView also?

2 Answers, 1 is accepted

Sort by
0
Ristogod
Top achievements
Rank 2
answered on 08 Oct 2010, 09:08 PM
I figured it out. The default Binding Mode for Selecteditem must be something other than TwoWay. As soon as I set the Mode=TwoWay, everything started working correctly! This is great as we now are looking at a dependable solution we can purchase.



<t:RadTreeView Grid.Column="0" Grid.Row="2"
        MinHeight="100" MinWidth="100"
        DataContext="{Binding Source={x:Static SizingApp:ProjectManager.Instance}}"
        SelectedItem="{Binding CurrentSelected, Converter={StaticResource PSSpecConverter}, Mode=TwoWay}"
        SelectionMode="Single">
0
Accepted
Alex Fidanov
Telerik team
answered on 11 Oct 2010, 01:23 PM
Hello Ristogod,

The Default value of the Binding's Mode property varies based on the type of the dependency property. Usually user-editable properties (like Text|CheckBoxes) default to TwoWay bindings and the rest to OneWay. More information on this, you can find here. For RadTreeView.SelectedItem the binding mode would also default to OneWay so you have to explicitly set it to TwoWay, as you already have.

Please let us know if you need further assistance on the matter.

Best wishes,
Alex Fidanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
Ristogod
Top achievements
Rank 2
Answers by
Ristogod
Top achievements
Rank 2
Alex Fidanov
Telerik team
Share this question
or