wpfuser-controlstreeviewtreeviewitem

Double click TreeViewItem to show UserControl in MainWindow's Grid


I want to show a UserControl in MainWindow's Grid, but not working when double click it won't show the UserControl.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200" MaxWidth="400" MinWidth="0"/>
        <ColumnDefinition Width="0"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0" Margin="0,0,0,0">
        <TreeView Background="Transparent">
            <TreeViewItem Header="Purchase" IsExpanded="False">
                <TreeViewItem Header="Material" x:Name="TreeViewItem_Material" MouseDoubleClick="TreeViewItem_Material_MouseDoubleClick"></TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </Grid>
    <GridSplitter Margin="0,0" Width="5"/>
    <Grid Grid.Column="2" x:Name="MainGrid" Margin="0,0,0,0"></Grid>
</Grid>
private void TreeViewItem_Material_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
            UserControl material = new Material(); //UserControl: Material.xaml
            MainGrid.Children.Add(material);
    }

Thank you.

Edit----- My code running well, just UserControl Layout problem that's cause not showing.


Solution

  • Your code works well in my environment as shown below.

    The Hello, Material shows when double-click node Material.

    Check Material visibility or use a simple UserControl may helps.

    enter image description here