WPF - Binding ResourceDictionary.Source to a Resou

2019-03-01 23:35发布

The following View\ViewModel does not work (though I've tried):

Presenter:

class SelectListPresenter : INotifyPropertyChanged
{
    public SelectListPresenter()
    {
        //init code
    }
    ResourceDictionary PossibleValues { get; }
    ResourceDictionary AddedValues { get; }
}

View:

<UserControl ...>
    <UserControl.DataContext>
        <ViewModels:SelectListPresenter />
    </UserControl.DataContext>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries x:Key="Options">
                <ResourceDictionary Source="{Binding PossibleValues}" />
                <ResourceDictionary Source="{Binding AddedValues}" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <ComboBox ItemsSource="{StaticResource ResourceKey=Options}" />
</UserControl>

The code above, without edit, gives me a designer error Value Cannot be null<br />Parameter Name: item on only the first ResourceDictionary inside the MergedDictionaries Node.

error

Based on the Title, this Stack Overflow question from 2011 sounds the same as mine, but I get errors trying to add "x:Class" to any non-root element in the designer, and the accepted solution does not work for me. error2 It seems I could do this if I created a separate class for each of my possible resourcedictionaries, but that is what I'm trying to avoid.

I feel like I'm making it more complicated than it needs to be... Ideas?

0条回答
登录 后发表回答