WPF 控件分组

发布于 2024-10-12 05:47:39 字数 497 浏览 3 评论 0原文

我有一组控件,如下所示: <图片链接> 我重复使用了很多次。它非常简单,一个列表视图、3 个按钮和一些布局面板。

我想将其变成可重用的组件,但列表视图中的列可以更改,并且它们绑定的源也会更改。

我该怎么办?我已经看过 ContentTemplates 和 UserControls 等之间的许多比较,但它们似乎从来没有起作用(例如,添加将引发一个事件,我必须处理该事件才能将某些内容添加到列表视图,删除将引发一个我可能会问的事件如果他们首先确定的话)。

我已经使用自己的 UserControl 完成了这些事件,但无法将 GridViewColumns 列表传递给该控件。这也意味着我必须从 UserControl 手动公开 SelectedItem 等。子类化 Listview 似乎有利于设置和访问,但从概念上讲,在列表视图区域中拥有其他控件似乎并不正确。

正确的方法是什么?

I have a group of controls that look like this:
<Link to Image>
that i reuse a number of times. It's really simple a listview, 3 buttons and some layout panels.

I want to turn this into a reusable component but the columns in the listview can change and the sources they are bound to will change.

How do i go about this? i've seen many comparisons between ContentTemplates and UserControls etc but they never seem to be functional (eg Add will raise an event which i'll have to handle to add something to the listview, remove will raise an event where i'll likely ask if they are sure first).

I've accomplished the events with my own UserControl, but can't pass a list of GridViewColumns to the control. It also means i have to expose SelectedItem etc manually from the UserControl. Subclassing Listview seems promising for setup and access but doesn't conceptually seem right to have other controls in the listview area.

What is the right way?

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

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

发布评论

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

评论(1

抱着落日 2024-10-19 05:47:39

我肯定会推荐 UserControl。您应该:

  1. 将所需的控件添加到用户控件中
  2. 将所需的依赖属性添加到用户控件中,以便您对其进行配置,例如 SelectedItem
  3. 将这些依赖属性连接到用户控件中的各个控件。执行此操作的一个简单方法是将用户控件可视化树的 DataContext 设置为用户控件本身,例如,如果您有一个 Grid 作为用户控件的根,请在代码中设置其 DataContext = this。然后,您可以使用 TwoWay 绑定将各种控件属性连接到用户控件属性。

I would definately recommend a UserControl. You should:

  1. Add the controls you require to your user control
  2. Add the Dependency Properties you require to your user control which allow you to configure it, e.g. SelectedItem
  3. Wire up these dependency properties to the various controls within your user control. An easy way to do this is to set the DataContext of your user controls visual tree to the user control itself, e.g. if you have a Grid as the root for your user controls, set its DataContext = this in code. You can then use TwoWay bindings to connect up the various control properties to the user control properties.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文