TreeView not loaded after ItemContainerStyle

1 Answer 51 Views
Styling TreeView
Tadeáš
Top achievements
Rank 1
Tadeáš asked on 27 Jul 2024, 11:25 AM | edited on 06 Aug 2024, 01:30 PM

Hello,

I would like to ask for your help. When creating RadTreeView my ItemContainerStyle is not working. I would like to connect ViewModel IsExpanded with RadTreeViewItem but I can't do it. The component works fine for me if I define a HierarchicalDataTemplate for the Item template as follows.


   <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
       <Grid.Resources>

             <HierarchicalDataTemplate x:Key="ItemTemplateTree"
                                       ItemsSource="{Binding Children}">
                 <StackPanel Orientation="Horizontal">
                     <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                     <TextBlock Text="{Binding Value, StringFormat=': {0}'}" />
                 </StackPanel>
             </HierarchicalDataTemplate>
         </Grid.Resources>
 <telerik:RadTreeView  Name="JsonTreeView"
             ItemsSource="{Binding SelectedItemInContent.MediaTreeInfoJson}"
             IsVirtualizing="True"
             SelectionMode="Extended"
             ItemTemplate="{StaticResource ItemTemplateTree}"
            />

     </Grid>




 

But if I try to change the ItemContainerStyle like in the Demo examples "ShuttleControl_WPF", the whole tree disappears.  As if it doesn't have a properly defined Style.

 


<Style x:Key="nodeStyle" TargetType="telerik:RadTreeViewItem">
       <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
   </Style>

The model I use for Items is as follows:

 


 public class JsonInfoTreeModel: ViewModelBase
    {
        private string name;
        private string value;
        private ObservableCollection<JsonInfoTreeModel> children;
        private bool isExpanded;

        public JsonInfoTreeModel()
        {
            Children = new ObservableCollection<JsonInfoTreeModel>();
            IsExpanded = true;
        }

        public string Name
        {
            get { return name; }
            set
            {
                if (name != value)
                {
                    name = value;
                    OnPropertyChanged(nameof(Name));
                }
            }
        }

        public string Value
        {
            get { return value; }
            set
            {
                if (this.value != value)
                {
                    this.value = value;
                    OnPropertyChanged(nameof(Value));
                }
            }
        }

        public ObservableCollection<JsonInfoTreeModel> Children
        {
            get { return children; }
            set
            {
                if (children != value)
                {
                    children = value;
                    OnPropertyChanged(nameof(Children));
                }
            }
        }

        public bool IsExpanded
        {
            get { return isExpanded; }
            set
            {
                if (isExpanded != value)
                {
                    isExpanded = value;
                    OnPropertyChanged(nameof(IsExpanded));
                }
            }
        }
    }


Thank you for any help.

 

 

************** Update ****************

ExamleDesk.WPF.zip

 

 

1 Answer, 1 is accepted

Sort by
1
Accepted
Stenly
Telerik team
answered on 29 Jul 2024, 04:40 PM

Hello Tadeáš,

May I ask if you could give the below article a try? It shows the most common reasons why elements may be missing from the UI, as well as suggested steps for resolving them:

Missing Controls in the UI - Telerik UI for WPF

I hope the provided information will be of help to you.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tadeáš
Top achievements
Rank 1
commented on 31 Jul 2024, 07:49 AM

Hello,

Thank you for your reply.

I have already reviewed the article and tested all the things.

TreeView works fine until I try to bind the Style with to the Model.

Stenly
Telerik team
commented on 31 Jul 2024, 08:03 AM

Hello Tadeáš,

Could you share a sample project that showcases this behavior, so that I could review it?

Tadeáš
Top achievements
Rank 1
commented on 06 Aug 2024, 01:32 PM

Hello,
I have updated the question and added an example called SampleProject ExamleDesk.WPF.zip.
Thank you very much for your help.

Stenly
Telerik team
commented on 06 Aug 2024, 02:42 PM

Hello Tadeáš,

I have downloaded the sample project, however, I was not able to run it due to missing packages, as well as issues regarding the .csproj file.

However, I have also checked the implementation of the application, and judging by the logic in the App.xaml file, my guess is that the NoXaml assemblies are used on your end. When using this version, any custom Style that targets a Telerik element needs to be based on its default Style (using the BasedOn property) as shown in the below article:

Styling the Controls - Telerik UI for WPF

Otherwise, the element will not be visible in the UI.

The following code snippet shows how to base the custom x:Name="nodeStyle" Style on the default one:

<Style x:Key="nodeStyle" TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
</Style>

This is also mentioned in the Missing Controls in the UI article:

Missing Controls in the UI - Telerik UI for WPF

I hope the provided information will be of help to you.

Tadeáš
Top achievements
Rank 1
commented on 07 Aug 2024, 09:33 AM

Hello,

Thank you very much. It works very well. I also tried a similar thing before but nothing worked. I probably had another bug in the code. But now it works in the example .

Once again thank you very much, very good work.

Tadeáš

Tags
Styling TreeView
Asked by
Tadeáš
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or