数据绑定到动态加载插件中的函数

发布于 2024-08-18 22:47:02 字数 1266 浏览 2 评论 0原文

我有几个菜单项,其命令绑定到我的 ViewModel。直到今天,它们都正常执行。

现在我添加了一个 MenuItem,其 ItemsSource 绑定到 ObservableCollection。此 MenuItem 的要点是枚举插件列表,以便显示所有插件的名称。然后,当用户单击插件名称时,它应该调用一个函数来显示音频过滤器的属性。

在我当前的实现中,这不起作用,我尝试像这样进行数据绑定:

<MenuItem Header="Filters" ItemsSource="{Binding FilterPluginNames}">
    <MenuItem.ItemContainerStyle>
        <Style>
            <Setter Property="MenuItem.Command" Value="{Binding ShowFilterDialogCommand}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

问题是我收到 BindingExpression 路径错误,因为它尝试使用字符串作为 MenuItem 的 DataContext。

这让我相信 MenuItem 的 MenuItems 的 DataContext 会自动设置为 ItemsSource 中的对象类型。这是真的吗?

如果我需要更改 DataContext,那么我想将其更改为处理所有其他命令的 ViewModel。但如果我这样做,我到底如何才能知道我想要显示哪个插件的过滤器属性呢?我至少需要传递一个 CommandParameter,但将此值绑定到过滤器名称并不是我最喜欢的选项。还有其他方法可以做到这一点吗?

如果 DataContext 确实自动设置为 ObservableCollection 中的对象类型,那么我宁愿直接调用我的接口方法 ShowFilterProperties() 。我敢打赌,如果没有命令绑定,我就无法做到这一点。如果是这样的话,大家都是怎么处理这种申请的呢?您是否让所有插件都公开一个命令处理程序,然后该处理程序将显示对话框?

编辑 - 我修改了代码以更改 ObservableCollection 类型,果然,WPF 希望将数据绑定到类型 T。所以我想一个选择是让插件公开 ICommand,但我不这样做不知道这是否是一种奇怪的做法?

编辑——好吧,我刚刚学到了一些新东西。接口不能有字段,所以是不是不可能与插件进行数据绑定,就这样?

I have several MenuItems whose Commands are bound to my ViewModel. Until today, all of them execute properly.

Now I have added a MenuItem whose ItemsSource is bound to an ObservableCollection. The point of this MenuItem is to enumerate a list of plugins so that the name of all plugins show up. Then when the user clicks on a plugin name, it should call a function to display properties for audio filters.

In my current implementation, which doesn't work, I tried to databind like this:

<MenuItem Header="Filters" ItemsSource="{Binding FilterPluginNames}">
    <MenuItem.ItemContainerStyle>
        <Style>
            <Setter Property="MenuItem.Command" Value="{Binding ShowFilterDialogCommand}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

The problem is that I get a BindingExpression path error because it's trying to use a String as the MenuItem's DataContext.

This leads me to believe that the DataContext for a MenuItem's MenuItems is automatically set to type of objects in the ItemsSource. Is this true?

If I need to change the DataContext, then I'd like to change it to the ViewModel that handles all of my other Commands. But if I do that, how in the world am I able to tell which plugin I want to display filter properties for? I'd need to pass in a CommandParameter at the very least, but binding this value to the filter name isn't my most favorite option. Are there any other ways to do this?

If the DataContext is indeed automatically set to the object type in the ObservableCollection, then I'd rather just call my interface method ShowFilterProperties() directly. I bet that I can't do this without Command binding. If that is the case, how do you all deal with this sort of application? Do you make all of the plugins expose a command handler, which will then show the dialog?

EDIT -- I modified my code to change the ObservableCollection type, and sure enough, WPF wants to databind to the type T. So I guess one option is to have the plugin expose the ICommand, but I don't know if this is a weird approach or not?

EDIT -- ok, I just learned something new. Interfaces can't have fields, so is it not possible to databind with plugins, period?

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

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

发布评论

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

评论(1

壹場煙雨 2024-08-25 22:47:02

你可能并不像你想象的那样具有约束力。您可能只想对绑定进行一些诊断并查看它们绑定到什么对象。这是调试绑定的一个很好的链接:

http://www.beacosta.com/blog/ ?p=52

这是一个示例:

<Window …
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    />

    <TextBlock Text="{Binding Path=Caption, diagnostics:PresentationTraceSources.TraceLevel=High}" … />

我认为您的方法是正确的......它可能只需要稍微调试一下。

You are likely not quite binding like you think you are. You might want to just put some diagnostics on your bindings and see what object they are binding to. Here is a good link for debugging bindings:

http://www.beacosta.com/blog/?p=52

Here's a sample:

<Window …
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    />

    <TextBlock Text="{Binding Path=Caption, diagnostics:PresentationTraceSources.TraceLevel=High}" … />

I think your approach is correct... it probably just needs to be debugged a little.

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