0

I followed this Tutorial and got stuck on the Create Instance of Config Settings Class-part. Here they just add the ObjectDataProvider in the Application.Resources. In my case there is already a ResourceDictionary in this context and I think this causes the problem.

My App.xaml:

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:config="clr-namespace:MyApp.ViewModels"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
            <!-- create instance of config-settings class -->
            <ObjectDataProvider x:Key="AppSettingsDataProvider" ObjectType="{x:Type config:AppSettingsManager}"/>

            <ResourceDictionary x:Key="MainDictionary">
                <!-- also not working: -->
                <!--<ObjectDataProvider x:Key="AppSettingsDataProvider" ObjectType="{x:Type config:AppSettingsManager}"/>-->

                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/Folder/file1.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/Folder/file2.xaml" />
                    <!-- <ResourceDictionary Source="{Binding Source={StaticResource AppSettingsDataProvider}, Path=LoadMethod, Mode=OneWay}"/>-->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    </Application.Resources>
</Application>

When I run the program my ResourceDictionary is not loaded anymore (Application.Current.Ressources.MergedDictionaries is empty), without the ObjectDataProvider everything works fine. Putting the ObjectDataProvider in the ResourceDictionary like suggested in the post Problems adding an ObjectDataProvider in resources did not help (is it different in the Application-class?). So where to put the ObjectDataProvider?

Berger
  • 299
  • 3
  • 13
  • 1
    You can check the accepted answer of this question : https://stackoverflow.com/questions/8150401/how-can-i-bind-a-textblock-to-an-objectdataprovider-resource-defined-in-app-xaml – Aakanksha Nov 19 '19 at 05:19
  • Hm that works, thanks! But I have to write it at the very end, before ``. I just found out that if I put the *ObjectDataProvider* at the very end it works also. But my hope was that I can use this Binding already for the *ResourceDictionary* like in my outcommented code for the last entry of the *MergedDictionaries*. – Berger Nov 19 '19 at 06:24
  • You want to use a binding to an objectprovider which you're putting in resources in order to provide the text for a resource dictionary? This seems very strange. Why would you want to do that? – Andy Nov 19 '19 at 08:22
  • I wanted to add a different resource depending on a setting, but I solved it now by adding this resource in code which works perfectly:) – Berger Nov 19 '19 at 08:30

0 Answers0