WPF 树视图的不同级别的样式不同?

发布于 2024-07-30 12:16:45 字数 201 浏览 2 评论 0原文

我有一个非常扁平结构的树视图,只有两层项目 - 主项目和一层子项目。 我正在使用 WPF MVVM,并且想要一种以不同方式设置两个级别的样式的方法,但不知道如何实现。

我将树视图绑定到 ViewModel 中的 ObservableCollection,每个元素都有一个用于下一级别的 ObservableCollection。

有什么帮助吗?

I have a very flat-structured treeview with only two levels of items - the main ones, and one level of subitems. I am using WPF MVVM and would like a way to style the two levels differently, but have no idea how.

I bind the treeview to an ObservableCollection in my ViewModel, and each element has one more ObservableCollection for the next level.

Any help?

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

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

发布评论

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

评论(1

殤城〤 2024-08-06 12:16:45

这可以通过 DATABINDING 和使用 DATATEMPLATES 来完成。

您将设计两个数据模板。 1 个作为 Hierarchical DataTemplate,另一个作为较低级别的标准版本(这是因为您只使用 2 个级别)

然后将 HierarchicalDataTemplate 的 ItemTemplate 设置为常规 DataTemplate

详细信息可以在此处找到: http://msdn.microsoft.com/en-us/magazine/cc700358.aspx

代码片段上述网站:

 <!-- ORDER DETAIL TEMPLATE -->
    <DataTemplate x:Key="OrderDetailTemplate">
      <TextBlock>
        <Run>Product:</Run>
        <TextBlock Text="{Binding Path=Product}" />
        <Run>(</Run>
        <TextBlock Text="{Binding Path=Quantity}" />
        <Run>)</Run>
      </TextBlock>
    </DataTemplate>

    <!-- ORDER TEMPLATE -->
    <HierarchicalDataTemplate 
      x:Key="OrderTemplate"
      ItemsSource="{Binding Path=OrderDetails}"
      ItemTemplate="{StaticResource OrderDetailTemplate}"
      >
      <TextBlock Text="{Binding Path=Desc}" />
    </HierarchicalDataTemplate>

This can be accomplished via DATABINDING and using DATATEMPLATES.

You would design two DataTemplates. 1 as a Hierarchical DataTemplate and the other as a standard version for your lower level (this is since you only utilize 2 levels)

Then set the ItemTemplate of your HierarchicalDataTemplate to the regular DataTemplate

Details can be found here: http://msdn.microsoft.com/en-us/magazine/cc700358.aspx

Code snippet from the above site:

 <!-- ORDER DETAIL TEMPLATE -->
    <DataTemplate x:Key="OrderDetailTemplate">
      <TextBlock>
        <Run>Product:</Run>
        <TextBlock Text="{Binding Path=Product}" />
        <Run>(</Run>
        <TextBlock Text="{Binding Path=Quantity}" />
        <Run>)</Run>
      </TextBlock>
    </DataTemplate>

    <!-- ORDER TEMPLATE -->
    <HierarchicalDataTemplate 
      x:Key="OrderTemplate"
      ItemsSource="{Binding Path=OrderDetails}"
      ItemTemplate="{StaticResource OrderDetailTemplate}"
      >
      <TextBlock Text="{Binding Path=Desc}" />
    </HierarchicalDataTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文