Add collection or array to wpf resource dictionary

三世轮回 提交于 2019-12-03 03:14:41

As far as I understand your problem you want to bind a ComboBox (or a ListBox) with an array of items, right? If you want items from some external data source, you can just make use of handy DataContext property. Search MSDN for more on this. However, if you do want a manual collection, do it this way:

Create your own class:

public class StringCollection : ObservableCollection<string> { }

and now use it like this:

<Window.Resources>
    <local:StringCollection x:Key="stringCollection">
        <sys:String>Hello</sys:String>
        <sys:String>World</sys:String>
    </local:stringCollection>
</Window.Resources>

...

    <ListBox
        Margin="15"
        ItemsSource="{StaticResource stringCollection}" />

Or, if you want more generic collection create a class like this:

public class ObjectCollection : ObservableCollection<object> { }

and use it like this:

    <local:ObjectCollection x:Key="objectCollection">
        <sys:String>Hello</sys:String>
        <TextBlock>World</TextBlock>
        <sys:Int32>12345</sys:Int32>
    </local:ObjectCollection>

    ...

    <ComboBox
        Margin="15"
        ItemsSource="{StaticResource objectCollection}" />



You may also want to make use of some in-built classes like Int32Collection that implements IEnumerable. You can use them directly as a resource.


The Resources property (of any FrameworkElement like Window, UserControl, or Application) is of type ResourceDictionary not collection of resource dictionaries! so you can't do like this:

<UserControl.Resources>

    <!-- The first RD -->
    <!--<ResourceDictionary>
        <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
    </ResourceDictionary>-->

    <!-- Second RD!!! -->
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

Instead do like this:

<UserControl.Resources>

    <!-- 
        There should be only 1 main RD, 
        Merge other RDs, if any
     -->
    <ResourceDictionary>

        <!-- First Resource -->
        <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />

        <!-- Second Resource -->
        <ResourceDictionary.MergedDictionaries>
            <!-- Now, there can be multiple RDs -->
            <ResourceDictionary Source="/ResourcesA.xaml" />
            <ResourceDictionary Source="/ResourcesB.xaml" />
            <ResourceDictionary Source="/ResourcesC.xaml" />
            <ResourceDictionary Source="/ResourcesD.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

</UserControl.Resources>

Hope this helps.
Regards,
Mihir Gokani

Please correct me if I'm wrong but I think you want something like this?

<Grid.Resources>
  <local:MyCustomCollection x:Key="local:MyCustomCollection"/>
</Brid.Resources>

At the top of your window you'd have a property:

xmlns:local="clr-namespace:MyNamespace"

And inside your code you'd have:

MyCustomCollection  custCol = MyGrid.Resources["MyCustomCollection"] as MyCustomCollection;

Binding would happen on a control with a property something like this:

ItemsSource="{StaticResource MyCustomCollection}"

For the resources, you just need to mover your additional converter into the newly declared ResourceDictionary:

<App.Resources> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/ResourcesD.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 

    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" /> 
    </ResourceDictionary> 
</App.Resources> 

If you add resource dictionary inside Window's resources, you will not be able to access it everywhere, instead you should add in "App"'s resources (check out App.xaml file).

<App.Resources> 
    <win:BooleanToVisibilityConverter x:Key="VisibilityConverter" /> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/ResourcesD.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</App.Resources> 

This will be available in every WPF object regardless of creating any inheritance hierarchy.

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