访问 WPF 中 DataTemplate 内的项目

发布于 2024-08-08 19:12:08 字数 567 浏览 2 评论 0原文

我想知道在 WPF 中您是否能够获取数据模板对象的实际实例。例如,在以下情况下:

<UserControl>
    <UserControl.Resources>
        <DataTemplate x:Key="MyTemplate">
            <CustomControl ></CustomControl>
        </DataTemplate>
    </UserControl.Resources>

    <ListBox DataTemplate="{StaticResource MyTemplate}"></ListBox>
</UserControl>

假设 CustomControl 有一个 CustomEvent 和一个公共 CustomMethod。我想访问该事件和用户控件中的公共方法。这可能吗?我怎样才能做到这一点?预先感谢您的任何帮助。

干杯,

尼禄

I was wondering if in WPF you are able to get the actual instances of the datatemplate objects. For example in the following situation:

<UserControl>
    <UserControl.Resources>
        <DataTemplate x:Key="MyTemplate">
            <CustomControl ></CustomControl>
        </DataTemplate>
    </UserControl.Resources>

    <ListBox DataTemplate="{StaticResource MyTemplate}"></ListBox>
</UserControl>

Assume that CustomControl has a CustomEvent and a public CustomMethod. I want to access that event and the public method in the user control. Is this possible? How would I be able to do this? Thanks in advance for any help.

Cheers,

Nilu

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

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

发布评论

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

评论(3

别再吹冷风 2024-08-15 19:12:08

您需要找到包含 ListBox 的 ContentPresenter(通过导航 VisualTree),然后使用

myDataTemplate.FindName("myCustomControl", myListBox);

MSDN 上有一个示例: http://msdn.microsoft.com/en-us/library/bb613579.aspx

You need to find the ContentPresenter holding the ListBox (by navigating the VisualTree) and then use

myDataTemplate.FindName("myCustomControl", myListBox);

There is an example on MSDN: http://msdn.microsoft.com/en-us/library/bb613579.aspx.

懒猫 2024-08-15 19:12:08

我在 ListBox 上没有看到 ItemsSource 数据绑定,因此我假设您将其遗漏了。如果你绑定到像 ObservableCollection<> 这样的东西那么 ListBox 中的每个项目都会有它自己的 ViewModel 类。您可以根据需要使用公共方法。

如果您希望处理自定义控件中的事件,请在最低级别的代码隐藏中处理它,在本例中是在 UserControl 的代码隐藏中。

然后,在每个 ViewModel 中都有一个 ICommand 实例(或者路由命令,如果适合您的目的)。在 UserControl 中,您有一个 DataContext,您可以将其转换为 ViewModel 的类型。因此事件处理程序可以访问 ViewModel 并执行命令。

这是 Josh Smith 关于路由命令的文章,您可能会看到 定义

在这篇关于采用 MVVM 架构的应用的文章中,Josh 描述了自 ICommands

(这是伪代码)

class ViewModelType {
    public void DoSomething() { /* ... */ }
    public ICommand DoSomethingCommand { get; set; }
    public string Property { get; set; }
}

class CodeBehind {
    public void EventHandler(object, args) {
        (DataContext as ViewModelType).DoSomethingElseCommand.Execute();
    }
}

I don't see the ItemsSource databinding on the ListBox, so I'm assuming you left it out. If you bind to something like an ObservableCollection<> then each item in the ListBox will have it's own ViewModel class. You may have public methods on those as much as you like.

If you want an event in the custom control to be handled, handle it in code-behind in the lowest level you can, in this case in the code-behind of the UserControl.

Then, in each ViewModel have an ICommand instance (or a routed command if that suits your purpose). In the UserControl you have a DataContext which you can cast to the type of your ViewModel. So the event handler can access the ViewModel and execute Commands.

Here is Josh Smith's article on Routed Commands which you might find interesting

In this article on Apps with MVVM architecture, Josh described custom ICommands

(This is pseudo-code)

class ViewModelType {
    public void DoSomething() { /* ... */ }
    public ICommand DoSomethingCommand { get; set; }
    public string Property { get; set; }
}

class CodeBehind {
    public void EventHandler(object, args) {
        (DataContext as ViewModelType).DoSomethingElseCommand.Execute();
    }
}
总以为 2024-08-15 19:12:08

您可以创建一个附加到 CustomControl 并与其交互的对象。

这篇博文阐述了一些我们可以扩展的有用概念: 具有附加行为的 Silverlight 的 ICommand

因此,您可以创建一个附加到自定义控件的类,而不是附加到按钮的单击事件(在 WPF 中已经有一个命令)。

按照引用的博客文章中的模式,您最终会得到:

<CustomControl 
  MyNamespace:CustomControlCommand.EventCommand=
  "{Binding Path=CommandHandler}" />

这将使您可以通过将 CustomControl 的事件转换为命令来访问它们。

You can create an object which attaches to the CustomControl and interacts with it.

This blogpost here illustrated some useful concepts that we can expand upon: ICommand for Silverlight with Attached Behaviors

So instead of attaching to the click event of a button (which in WPF already has a command anyways) you can create a class which attaches to your custom control.

Following the pattern in the referenced blog post you would end up with:

<CustomControl 
  MyNamespace:CustomControlCommand.EventCommand=
  "{Binding Path=CommandHandler}" />

This would give you access to the events of the CustomControl by turning them into commands.

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