'How Do I Define WPF Treeview Style and Event Separately?

I am using the following code for the Treeview object:

<TreeView x:Name="RegisteredServer" ItemsSource="{Binding RegisteredServerList}" Grid.Row="1">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <Image Margin="0,0,4,0" Source="{Binding Icon}" Width="16" Height="16" />
                    <TextBlock Text="{Binding Name}" FontWeight="SemiBold" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="Foreground" Value="{x:Reference Name=LabelForeground}"></Setter>
                <Setter Property="IsExpanded" Value="True"/>
                <EventSetter Event="PreviewMouseRightButtonDown"
                                 Handler="OnPreviewMouseRightButtonDown" />
                <EventSetter Event="Selected" Handler="Tree_SelectedItemChanged"/>
                <EventSetter Event="MouseDoubleClick" Handler="OnItemMouseDoubleClick"/>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>

And I have the following code in the style file named Style.xaml:

<Style TargetType="TreeView">
        <Setter Property="Control.Background" Value="Red"></Setter>
        <Style.Resources>
            <Style TargetType="TreeViewItem">
                <Setter Property="Background" Value="Orange"></Setter>
                <Setter Property="Foreground" Value="White"></Setter>
            </Style>
        </Style.Resources>
    </Style>

Since I use in the part where I assign the event, the code in my style file does not work.

I cannot write event and style codes together in because I use theme structure.

How should I proceed here? Thank you from now.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source