如何创建一个下拉树视图的 silverlight 组合框?

发布于 2024-09-06 03:42:04 字数 1057 浏览 0 评论 0原文

我正在尝试创建一个用户控件,它是一个组合框,打开时会显示层次结构数据的树视图。

我创建了用户控件并将 Popup 中模板的一部分替换为:

    <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
         <sdk:TreeView x:Name="Tree">
         </sdk:TreeView>
     </ScrollViewer>

但是,我不确定如何启用对此的绑定。树视图需要绑定到与组合框不同的数据上下文。我尝试在用户控件上实现 DependencyProperty ,这将允许我设置数据上下文,但我绝对不会以正确的方式进行操作。此时,我得到的只是一个空的树视图。

对此的任何帮助将不胜感激。

PS 一个额外的警告是我需要像这样模板化树视图:

 <sdk:TreeView x:Name="Tree">
   <sdk:TreeView.ItemTemplate>
     <sdk:HierarchicalDataTemplate ItemsSource="{Binding ChildUnits}">
       <StackPanel Orientation="Vertical" Width="200">
          <TextBlock x:Name="name" TextWrapping="Wrap" Text="{Binding Name}" FontWeight="Bold" />
          <TextBlock x:Name="type" Text="{Binding Id}" FontStyle="Italic" FontSize="10" Foreground="Gray" />
       </StackPanel>
     </sdk:HierarchicalDataTemplate>
  </sdk:TreeView.ItemTemplate>
</sdk:TreeView>

I'm trying to create a user control that is a combobox that, when opened, presents a treeview of heirarchal data.

I created the user control and replaced a portion of the template in Popup with:

    <ScrollViewer x:Name="ScrollViewer" BorderThickness="0" Padding="1">
         <sdk:TreeView x:Name="Tree">
         </sdk:TreeView>
     </ScrollViewer>

However, I'm not sure how to enable binding on this. The treeview needs to be bound to a different datacontext than the combobox. I tried implementing a DependencyProperty on the user control that would allow me to set the datacontext, but I'm definitely not going about it the right way. At this point, all I get is an empty treeview.

Any help on this would be greatly appreciated.

P.S. One additional caveat is that I need to template the treeview like so:

 <sdk:TreeView x:Name="Tree">
   <sdk:TreeView.ItemTemplate>
     <sdk:HierarchicalDataTemplate ItemsSource="{Binding ChildUnits}">
       <StackPanel Orientation="Vertical" Width="200">
          <TextBlock x:Name="name" TextWrapping="Wrap" Text="{Binding Name}" FontWeight="Bold" />
          <TextBlock x:Name="type" Text="{Binding Id}" FontStyle="Italic" FontSize="10" Foreground="Gray" />
       </StackPanel>
     </sdk:HierarchicalDataTemplate>
  </sdk:TreeView.ItemTemplate>
</sdk:TreeView>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文