WPF - 将ResourceDictionary.Source绑定到Presenter \ DataContext的ResourceDictionary属性

时间:2016-05-04 03:48:49

标签: wpf xaml mvvm data-binding resourcedictionary

以下View \ ViewModel不起作用(虽然我已尝试过):

主讲人:

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

查看:

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

上面的代码,没有编辑,只在MergedDictionaries节点内的第一个ResourceDictionary上给出了设计器错误Value Cannot be null<br />Parameter Name: item

error

基于标题,this Stack Overflow question from 2011听起来与我的相同,但是我在设计器中的任何非根元素添加“x:Class”时遇到错误,并且接受的解决方案对我不起作用。 error2 如果我为每个可能的资源都创建了一个单独的类,我似乎可以这样做,但这就是我想要避免的。

我觉得我让它变得比它需要的更复杂......想法?

0 个答案:

没有答案