我必须构建 ControlTemplate 吗? 或者还有其他选择吗?

发布于 2024-07-21 08:08:00 字数 2348 浏览 4 评论 0原文

我有一个 TreeView 并想要显示嵌套的数据(不是分层的)。 第一级数据称为TaskViewModel,第二级数据称为ArtifactViewModel。 我想要 ArtifactViewModel 水平位于代表 TaskViewModelGroupBox 内。 我尝试了不同的方法,这是我的最后一个:

<TreeView Name="tvTasks" ItemsSource="{Binding Tasks}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type vm:TaskViewModel}">
            <GroupBox Header="{Binding Name, UpdateSourceTrigger=PropertyChanged}">
                <StackPanel Orientation="Vertical">
                    <ListView ItemsSource="{Binding Children}"/>
                    <TextBlock Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"/>
                </StackPanel>
            </GroupBox>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type vm:ArtifactViewModel}">
            <Border Background="{Binding Type,Converter={StaticResource Type2Background}}"
                    Margin="5" BorderBrush="Black" BorderThickness="2" CornerRadius="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80"/>
                        <RowDefinition Height="20"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                               TextAlignment="Center" Background="Black" Foreground="White"
                               Opacity="0.75" Grid.Column="0" Grid.Row="1"/>
                </Grid>
            </Border>
        </DataTemplate>                                          
    </TreeView.Resources>
</TreeView>

这看起来非常像我想要的,除了 ArtifactViewModel 是垂直显示的。 如果我点击 ArtifactViewModel tvTasks.SelectedItem 不会改变,因为 ListView 处理此事件。 我知道这种方法不是最聪明的,但这只是一种尝试。
我看了这篇文章,但我不知道如何处理我想放入 TreeView 中的不同对象。 那么...我该如何构建这样的用户界面呢?

I got a TreeView and want to display the Data nested (not hierarchically). The first level data is called TaskViewModel, the second level data is ArtifactViewModel. I want the ArtifactViewModel horizontal inside a GroupBox which represents TaskViewModel.
I tried different approaches, this is my last one:

<TreeView Name="tvTasks" ItemsSource="{Binding Tasks}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type vm:TaskViewModel}">
            <GroupBox Header="{Binding Name, UpdateSourceTrigger=PropertyChanged}">
                <StackPanel Orientation="Vertical">
                    <ListView ItemsSource="{Binding Children}"/>
                    <TextBlock Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap"/>
                </StackPanel>
            </GroupBox>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type vm:ArtifactViewModel}">
            <Border Background="{Binding Type,Converter={StaticResource Type2Background}}"
                    Margin="5" BorderBrush="Black" BorderThickness="2" CornerRadius="2">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80"/>
                        <RowDefinition Height="20"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
                               TextAlignment="Center" Background="Black" Foreground="White"
                               Opacity="0.75" Grid.Column="0" Grid.Row="1"/>
                </Grid>
            </Border>
        </DataTemplate>                                          
    </TreeView.Resources>
</TreeView>

This looks pretty much like what i want, besides that ArtifactViewModels are shown vertical. And if i click on ArtifactViewModel the tvTasks.SelectedItem doesn't change, because the ListView handels this event. I know that this approach is not the cleverest, but it's just a try.
I looked at this article, but i don't see how to deal with the different objects i want to put in the TreeView. So ... how do i build such a UI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2024-07-28 08:08:00

您在这里遇到的主要问题是您嵌套了多个控件,每个控件都有自己的选定项目。

如果您计划以嵌套方式显示数据,但不按层次结构显示数据,则不必费心使用 TreeView。 如果您希望在任何给定时间点都可以选择一项,请改用列表框。

现在棘手的部分是考虑如何布置项目。 查看 Bea Stollnitz 的示例此处,她将 ListBox 重绘为 Canvas。 您可以执行类似的操作,其中 ItemsPanelTemplate 是 Canvas,然后计算 x,y 坐标。 或者,您可以使用 Grid,并确定 Grid.Row 和 Grid.Column 值。

The main issue you're running into here is that you're nesting multiple controls, each with their own selected items.

If you're planning on showing the data as nested but not hierarchically, don't bother using TreeView. If you want to have one item be selectable at any given point in time, use a ListBox instead.

Now the tricky part is playing around with how you want to lay the items out. Take a look at Bea Stollnitz's example here where she redraws a ListBox as a Canvas. You could do something similar where the ItemsPanelTemplate is a Canvas, and you calculate the x,y coordinates. Alternately, you could use a Grid, and determine the Grid.Row and Grid.Column values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文