I am building a WPF application with a bunch of connection strings loaded in the app.config file. What I'm trying to do is to get the WPF window's combobox to display the names of the connection strings, without having to also add these to an application setting. Is this possible?
In MainWindow.xaml:
<ComboBox Grid.Row="0" Grid.Column="1"
Name="Servers"
ItemsSource="{Binding ?app.config?}" />
In App.config:
<connectionStrings>
<add name="Prod" connectionString="Data source=..." />
<add name="Test" connectionString="Data source=..."/>
</connectionStrings>
EDIT:
This is the final process I used:
In my Window tag:
xmlns:m="clr-namespace:SqlWindow"
<Window.DataContext>
<m:MainWindowViewModel />
</Window.DataContext>
Then, within the main window XAML, I have:
<ComboBox Grid.Row="0" Grid.Column="1" Name="ServersComboBox"
ItemsSource="{Binding ConnectionStrings}"
DisplayMemberPath="Name"
SelectedIndex="0" />
In a separate class, I have:
public class MainWindowViewModel {
public IEnumerable<ConnectionStringSettings> ConnectionStrings {
get {
foreach (ConnectionStringSettings cn in ConfigurationManager.ConnectionStrings) {
yield return cn;
}
}
}
}
All of this got me to where I needed to be.