我刚刚开始使用c#和Wpf。我正在尝试构建一个实用程序,该实用程序从活动目录获取计算机,在树状视图中显示它们,然后通过WMI恢复信息。我已经成功地完成了大部分工作,而没有提出任何问题。但是,现在我的演示文稿有问题。Current Wpf output
选择所需的信息(磁盘/打印机/服务等)后,我想在右侧面板中显示该信息。我的问题是此信息可能是选项卡式表单,列表框,Gridview或graphic.ex A tab view什么是获得此结果的最佳方法。提前致谢。

“”当前占用的区域应显示不同的格式。



<StackPanel>
     <DockPanel Margin="3">
        <Border CornerRadius="6"
            BorderBrush="Gray"
            Background="LightGray"
            BorderThickness="2" >
            <StackPanel  Background="SkyBlue" Height="80">
                <StackPanel  Orientation="Horizontal"  VerticalAlignment="Center">
                    <Label Content="Domaine:"  HorizontalAlignment="Left" Height="30"/>
                    <TextBox Name="FrmDomain" HorizontalAlignment="Left" Height="30"  Width="80"/>

                    <Label Content="Zone" Height="30"/>
                    <TextBox Name="FrmTreeZone" HorizontalAlignment="Left" Height="30" Width="80"/>

                    <Label Content="Ordinateur:" />
                    <TextBox Name="FrmTreeOrdi" HorizontalAlignment="Left" Height="30" Width="80"/>

                    <Label Content="Option:" />
                    <TextBox Name="FrmTreeOption" HorizontalAlignment="Left" Height="30" Width="80" />
                    <Label Content="Disponible:" />
                    <TextBlock Name="FrmTreeAlive" HorizontalAlignment="Left" Height="30" Width="40"/>
                </StackPanel>

                <StackPanel  Orientation="Horizontal" VerticalAlignment="Center">
                    <Label Content="Recherche:"/>
                    <TextBox x:Name="FrmRecherche" HorizontalAlignment="Left" Height="30" Width="80"/>
                    <Button x:Name="btnRech" Click="btnRech_MouseClick" >
                        <materialDesign:PackIcon Kind="Search"/>
                    </Button>
                    <Label Content="Mac:" />
                    <TextBox x:Name="FrmMac" HorizontalAlignment="Left" Height="30" Width="80"/>
                </StackPanel>
            </StackPanel>
        </Border>
        </DockPanel>

    <DockPanel Margin="3">
        <Border CornerRadius="6"
            BorderBrush="Gray"
            Background="LightGray"
            BorderThickness="2" >
            <StackPanel Orientation="Vertical" HorizontalAlignment="Left"  Background="LightCyan" MinWidth="300" Width="300">
             <TreeView Name="FrmTreeView" Height="540" Margin="8"
                      SelectedItemChanged="TreeView_SelectedItemChanged"
                      PreviewMouseRightButtonDown="OnPreviewMouseRightButtonDown"
                      TreeViewItem.Selected="NodeSelected">
                <TreeView.Resources>
                    <ContextMenu x:Key="TestMenu">
                    </ContextMenu>

                    <Style TargetType="TreeViewItem" >
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Image Width="20" Margin="3" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
                                            AncestorType={x:Type TreeViewItem}},
                                            Path=Tag,
                                            Converter={x:Static local:HeaderToImageConverter.Instance }}"/>
                                        <TextBlock VerticalAlignment="Center" Text="{Binding}"/>
                                    </StackPanel>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TreeView.Resources>
            </TreeView>
    </StackPanel>
        </Border>

        <StackPanel  Name="test" DockPanel.Dock="Right"  Visibility="Visible">
            <Border CornerRadius="6"
            BorderBrush="Gray"
            Background="WhiteSmoke"
            BorderThickness="2" >

                <TextBlock Name="FrmActionAffiche"  Height="540" Margin="8"/>

            </Border>
            <DataGrid Height="1" Name="FrmDataGrid"/>

        </StackPanel>
    </DockPanel>

    <StackPanel Orientation="Horizontal">
        <Button Name="btnDisque" Content="Disque" Height="30" Width="100" Click="btnDisque_MouseClick" />
    </StackPanel>

    <StackPanel Orientation="Vertical" Height="30">
        <StatusBar>
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="4*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem>
                <TextBlock>Ready</TextBlock>
            </StatusBarItem>
            <StatusBarItem Grid.Column="1">
                <ProgressBar Value="30" Width="80" Height="18"/>
            </StatusBarItem>
            <StatusBarItem Grid.Column="2">
                <TextBlock>Set</TextBlock>
            </StatusBarItem>
            <StatusBarItem Grid.Column="3">
                <TextBlock>Go!</TextBlock>
            </StatusBarItem>
        </StatusBar>
    </StackPanel>

</StackPanel>

最佳答案

为了能够以不同的方式显示不同类型的信息,您应该使用一个DataTemplate,您可以有多个DataTemplates并根据类型选择显示哪个DataTemplates,而无需任何其他编码(或编写自己的逻辑以选择模板DataTemplateSelector)。

例如,假设左侧的TreeView包含类型为Computer和Printer的项目,则可以将控件绑定到TreeView的SelectedItem属性(显示当前选定的项目),然后将Type = Printer和Type = Computer的DataTemplates添加到该控件中描述您要如何显示它们之一。

如果您发布当前的代码(XAML和C#),我将很乐意为您提供具体案例的示例。

09-11 11:35