Silverlight ItemsControl 一次显示一项?

发布于 2024-11-24 17:47:20 字数 899 浏览 0 评论 0原文

我正在寻找一种在 Silverlight 中一次显示集合中的一个项目的好方法,而不显示用于选择项目的 UI。这就像没有选项卡的 TabControl,或者没有下拉菜单的组合框。

现在我们使用 TabControl 样式来隐藏选项卡,但这感觉就像黑客。在 Silverlight 中是否有更自然的方法来做到这一点?

以下是我正在寻找的功能:

  • 仅显示所选项目。
  • 不显示用于选择项目的 UI。 (选择将根据应用程序 UI 不同部分中的用户操作进行更改。)
  • 数据绑定到视图模型集合。
  • 使用 DataTemplate 显示每个视图模型的视图。
  • 保留每个选定项目的视图状态。 (例如,假设在一次一个项目的控件中,我们显示所选项目的选项树。我希望为每个项目单独跟踪树节点的展开/折叠状态。)

我尝试过仅使用绑定到所选项目的 ContentPresenter:

<ContentPresenter Content="{Binding SelectedItem}">
  <ContentPresenter.ContentTemplate>
    <DataTemplate>
      <MyUserControl />
    </DataTemplate>
  </ContentPresenter.ContentTemplate>
</ContentPresenter>

但这似乎使用 MyUserControl 的单个实例,当 SelectedItem 更改时将同一实例重新绑定到不同的视图模型。这意味着不会针对每个选定项目单独跟踪诸如树节点是否展开之类的状态。


有更好的方法吗?

感谢您的帮助,
理查德

I'm looking for a good way to display one item out of a collection at a time in Silverlight, without showing a UI for selecting an item. This would be like a TabControl without the tabs, or a combobox without the drop-down.

Right now we are using a TabControl styled to hide the tabs, but that feels like a hack. Is there a more natural way to do this in Silverlight?

Here are the features I'm looking for:

  • Display only the selected item.
  • Don't display a UI for selecting an item. (Selection will be changed based on user actions in a different part of the app's UI.)
  • Data binding to a collection of viewmodels.
  • Using a DataTemplate to display a view for each viewmodel.
  • Preserving the state of the view for each selected item. (For example, suppose that inside our one-item-at-a-time control, we display a tree of options for the selected item. I'd like the expanded/collapsed state of the tree nodes to be separately tracked for each item.)

I've tried just using a ContentPresenter bound to the selected item:

<ContentPresenter Content="{Binding SelectedItem}">
  <ContentPresenter.ContentTemplate>
    <DataTemplate>
      <MyUserControl />
    </DataTemplate>
  </ContentPresenter.ContentTemplate>
</ContentPresenter>

But this seems to use a single instance of MyUserControl, rebinding the same instance to different viewmodels as the SelectedItem changes. This means state such as whether or not tree nodes are expanded is not tracked separately per selected item.

Is there a better way to do this?

Thanks for your help,
Richard

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

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

发布评论

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

评论(3

哆啦不做梦 2024-12-01 17:47:20

做到这一点的最佳方法需要一些工作:

创建一个扩展 System.Windows.Controls.Primitives.Selector 的类,它是具有以下对象的基类:

  • 集合
  • 绑定到selectedItem 的

您将 Grid 设置为 ItemsPanel 并设置为ItemTemplate 填充整个网格的 DataTemplate,但默认隐藏(或无不透明度)。 DataTemplate 中 IsSelected 属性上的触发器隐藏/显示项目。

只有一种可能

The best way to do it needs some few work:

you create a class which extends System.Windows.Controls.Primitives.Selector which is the base class for objects which have:

  • A collection bound to
  • A selectedItem

you set a Grid as ItemsPanel and as ItemTemplate a DataTemplate which fills the whole grid, but is default hidden (or no opacity). A trigger in the DataTemplate on the IsSelected Property hides/showes the Item.

Just one possibility

巷子口的你 2024-12-01 17:47:20

这做起来相当简单。

第一步是使用 ListBox,提供数据模板、视图模型集合并绑定所选项目,就像您已经在做的那样。

您想要的步骤(隐藏所有未选定的项目)只需要您创建一个 ItemContainerStyle。这个样式有一个你要关注的VisualStateGroup,有“Selected”和“Unselected”两种状态。在 Unselected 状态下,您希望将 LayoutRoot 设置为折叠状态,在 Selected 状态下,您希望 LayoutRoot 可见。

如果您有 Blend,这将花费您大约 15 分钟。

This is rather simple to do.

First step is to use a ListBox, supply the datatemplate, viewmodel collection and bind the selected item like you're already doing.

The step you want (hiding all non selected items) merely needs you to create an ItemContainerStyle. This style has a VisualStateGroup you want to pay attention to, with two states "Selected" and "Unselected". Within the Unselected state you want to set the LayoutRoot to collapsed and within Selected you want the LayoutRoot to be visible.

If you've got Blend this'll take you about 15 minutes.

残疾 2024-12-01 17:47:20

您必须有权访问控件本身的 SelectedItem 属性是否有原因?如果您可以将处理所选项目的逻辑(例如取消选择、返回选择的项目等)移至视图模型,则可以仅使用带有 Grid 的 ItemsControl 作为 ItemsPanel。然后在 ItemTemplate 中您可以将 MyUserControl. MyUserControl 会将其 Visibility 属性绑定到数据项上的属性,并在两者之间设置转换器。然后,您可以在数据项而不是 UI 控件上将 IsSelected 设置为 true,并且将显示 MyUserControl 的相应实例。

Is there a reason why you must have access to a SelectedItem propery on the control itself? If you can move the logic of handling the selected item (such as unselecting, returning which item is selected etc.) to the view model you could just use an ItemsControl with a Grid as ItemsPanel. Then in the ItemTemplate you could put MyUserControl. MyUserControl would have its Visibility property bound to a property on the data item with a converter in between. Then you would set IsSelected to true on the data item instead of the UI control, and the corresponding instance of MyUserControl would be shown.

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