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