0

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.

Dan Champagne
  • 890
  • 2
  • 17
  • 37
  • so are you proposing to store the connection strings in a database and retrieve them that way..? What's wrong with storing them in the app.config file..? also you could store them in the Setting.Settings but you would still need to type in the values manually.. you could create a List and add the values to the list and then Bind the ComboBox to that List for example there are several ways you could approach this – MethodMan Dec 22 '14 at 17:51
  • No, I'm going to have them in app.config. I'm trying to bind the combobox's itemssource to just the name property of the connection strings. So from the above example, the combobox's items should say "Prod" and "Text". – Dan Champagne Dec 22 '14 at 17:53
  • try looking here based on my previous suggestion http://stackoverflow.com/questions/845030/bind-to-a-value-defined-in-the-settings – MethodMan Dec 22 '14 at 17:56
  • I know I can do this through an application setting. That's the method I'm trying to avoid, because then I'll have to have the connection string namnes loaded in app.config, and also in the application settings. I'm trying to avoid duplication of effort. – Dan Champagne Dec 22 '14 at 17:57

2 Answers2

1

You should create a backing property for Connections which you'll get from ConfigurationManager.Connections. Then bind it to the Combobox:

public IEnumerable Connections
{
   get
   {
       return ConfigurationManager.ConnectionStrings;
   }
}

the last thing left to do is to show proper connection name. To do that you have to specify DisplayMemberPath:

<ComboBox Grid.Row="0" Grid.Column="1"
          Name="Servers"
          DisplayMemberPath="Name"
          ItemsSource="{Binding Connections}">
</Combobox>


How this works?

ItemsSource property of Combobox is IEnumerable which allows to get DataContext for each item in the list. If you haven't specified DataTemplate explicitly - it will try to cast it(context) to string. For ConnectionStringSettings class this won't be something we really want to see. So we need to explicitly define how template should look like.
The easiest way is to set name of the property in the DisplayMemberPath. You can also override DataTemplate which is good for non-trivial cases.

Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
0

If you are not using MVVM pattern or any particular object to bind, you can do something like this:

In the code to begin of your window, you set the DataContext Property this way

public MainWindow()
{
  InitializeComponent();
  var connections = System.Configuration.ConfigurationManager.ConnectionStrings;
  DataContext=connections;
}

Then, in your window do this:

 <ComboBox Grid.Row="0" Grid.Column="1"
      Name="Servers"
      ItemsSource="{Binding}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
avenet
  • 2,894
  • 1
  • 19
  • 26
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • I know how to do that, but how would I bind the ConnectionString Name property in XAML to the ComboBox ItemsSource? That's what I'm after. – Dan Champagne Dec 22 '14 at 17:58
  • are you using MVVM pattern or just set the window datacontext in the code begin? – ocuenca Dec 22 '14 at 18:02