4

In my DB I have a table that contains different items of userControls with the attributes "ClassName", "AssemblyName" and "NameSpace" which are necesarry to init the instances using reflection.

My Idea was To get this collection from the DB, set the collection as the data-context and dynamically load these usercontrols into a tabcontrol. I could use a "tabItem" which would contain it and in runtime in the code behind load it. I guess it would be really handy and fancy if it could be done directly from XAML in a template.

I've been googleling for something similar, but found nothing without using code behind.

I was thinking something like the following

        <TabControl.ContentTemplate>
            <DataTemplate>
                <xxxControl ClassName="{Binding ClassName}" AssemblyName="{Binding AssemblyName}" NameSpace="{Binding NameSpace}" />
            </DataTemplate>
        </TabControl.ContentTemplate>

I could make such a custom "xxxControl" but it would be a waste of time if something like that already exists. This way The GUI could be completly generated by the parameters in the DB.

4

2 回答 2

1

您可以使用标记扩展在 XAML 中做很多事情,在这种情况下,您可以创建一个根据给定信息实例化控件的功能。为此,它需要一些可以绑定的依赖属性,然后在ProvideValue其中获取程序集,构造全名并实例化它。

用法:

<DataTemplate>
    <me:Instance Assembly="{Binding AssemblyName}"
                 NameSpace="{Binding NameSpace}"
                 Class="{Binding ClassName}"/>
</DataTemplate>

显然你仍然有代码隐藏,但它应该是这样的,命令式代码根本属于 XAML。

我也怀疑您的数据库是否应该包含有关 UI 控件的信息...

于 2013-05-15T12:37:03.123 回答
1

啊。不要直接从数据库控制您的 UI。您应该最接近的方式(假设您不能进行重大的架构更改)IMO 是将您的数据库条目加载到IObservable您的 VM 中,并使用DataTemplateSelector将您的集合转换为 UI 控件。

于 2013-05-15T18:50:47.793 回答