Link to home
Start Free TrialLog in
Avatar of kravindra
kravindra

asked on

How to expand and select first treeview item by default in treeview control in WPF

<TreeView Height="288" HorizontalAlignment="Left" Margin="18,23,0,0"  VerticalAlignment="Top" Width="89"
                  x:Name="tvCita" ItemsSource="{Binding CitaCol,Mode=TwoWay}" ItemTemplate="{StaticResource MasterTemplate}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectedItemChanged">
                    <cmd:EventToCommand Command="{Binding TreeviewItemClick}" CommandParameter="{Binding  SelectedItem, ElementName= tvCita, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
 </TreeView>
<DataTemplate x:Key="DetailTemplate">
            <TextBlock Text="{Binding Swift_File_Name}"/>
        </DataTemplate>
           
        <HierarchicalDataTemplate x:Key="MasterTemplate"
                                  ItemsSource="{ Binding SwiftCol,Mode=TwoWay}"
                                  ItemTemplate="{StaticResource DetailTemplate}">
            <TextBlock Text="{Binding CITA_Info_Id}"/>
        </HierarchicalDataTemplate>

I tried like this , but I am getting error

I created style under window.resource

<Style TargetType="TreeViewItem">      
<Setter Property="IsExpanded" Value="True" />  
</Style>  

I called this style in treeview control like this style="<staticResource TreeViewItem>

but i am getting runtime exception like this

'Set property 'System.Windows.FrameworkElement.Style' threw an exception.' Line number '40' and line position '79'.

How to fix it or How to expand and select first treeview item by default in treeview control in WPF
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial