WPF - Binding ResourceDictionary.Source to a ResourceDictionary Property of the Presenter\DataContext

自古美人都是妖i 提交于 2019-12-02 07:58:56

问题


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.

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. 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?

来源:https://stackoverflow.com/questions/37018071/wpf-binding-resourcedictionary-source-to-a-resourcedictionary-property-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!