Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a custom WPF usercontrol in one project that reads values from an app.conf, which is does just fine. However, when you attempt to create a new project that uses this control, the values are all null and my custom WPF usercomtrol project dies because it needs those values.

What I have tried:

I have tried modifying my csproj for it to be a Resource, EmbeddedResouce, copytoOut...always....Nothing
Posted
Comments
Dave Kreskowiak 26-Jul-24 19:24pm    
Your control doesn't get it's own app.config. It has to read the app.config supplied by the application using the control. If you don't put the values your control needs into the parent app.config, it's going to fail.
John Grove from Unknown 29-Jul-24 9:57am    
Thanks Dave

1 solution

Step 1: Make a WPF Class Library project to contain the class you want to create.
Here I create a new control, that inherits from Button, but sets its content from a config value as you want (exception handling etc. not shown):

(Make sure your project references System.Configuration)
using System.Configuration;
using System.Windows.Controls;

namespace AppConfigControl
{
    public class AppConfigButtonControl : Button
    {
        public AppConfigButtonControl() : base()
        {
            var configValue = ConfigurationManager.AppSettings["ButtonContent"];
            if (null != configValue)
            {            // Example: Display the setting in a TextBlock
                var textBlock = new TextBlock { Text = configValue };
                this.Content = configValue;
            }
        }
    }
}


Make a new (consumer) wpf project that either refrences the AppConfigControl control or its compiled dll. Make a view that contains your new control:

Window x:Class="AppConfigControlConsumer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AppConfigControlConsumer"
        xmlns:controls="clr-namespace:AppConfigControl;assembly=AppConfigControl"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <controls:AppConfigButtonControl Margin="20" Height="40" Width ="150"/>
    </Grid>
</Window>

- Notice the line: xmlns:controls="clr-namespace:AppConfigControl;assembly=AppConfigControl"

Make a new App.xaml file for your configuration:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings>
	<add key="ButtonContent" value="Config text value"/>
	</appSettings>
</configuration>


Run and enjoy
 
Share this answer
 
Comments
0x01AA 17-Aug-24 12:22pm    
My 5, even I did not read all this. But I hate downvotes without comments ;)
Niels Peter Gibe 19-Aug-24 4:50am    
Thank you mr/mrs/miss/whatever 426.
I would also like to know, why (currently) two people has given this answer a vote of 1. Not that I care much, but I guess that if people keep doing that (voting 1 to answers without any kind of explanation) then it will only be a matter of time, before this lack of respect will result in that no one will submit answers to questions.
John Grove from Unknown 18-Aug-24 0:08am    
I didn't down vote anything. Anybody who takes to time to help someone else always gets a solid vote from me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900