0

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?

4

1 回答 1

1

您可能不像您认为的那样具有约束力。您可能只想对绑定进行一些诊断,然后查看它们绑定到的对象。这是调试绑定的好链接:

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}" … />

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

于 2010-01-27T17:52:36.617 回答