0

I have a combo box in my WPF project and I would like to have it items defined by a readonly string array in my configuration class. This way I would make it very easy to reconfigure the combo box items.

Is it possible to bind my ItemsSource property to a readonly string[]? How it can be done?

2
  • 4
    It is possible. What have you tried? Commented Mar 16, 2012 at 18:08
  • Sure I have tried, but I couldn't. That's why I would like some help here. Can you post the code? Commented Mar 16, 2012 at 18:11

2 Answers 2

3

MainWindow :

<Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication4" Title="MainWindow" Height="350" Width="525"> <StackPanel> <ComboBox ItemsSource="{Binding List, Source={x:Static local:Configuration.Instance}}"></ComboBox> </StackPanel> 

Configuration File :

public class Configuration { // Singleton private static Configuration _instance; public static Configuration Instance { get { if (_instance == null) _instance = new Configuration(); return _instance; } } public IEnumerable<string> List { get { return new List<string>() { "toto 1", "toto 2" }; } } public Configuration() { } } 
Sign up to request clarification or add additional context in comments.

3 Comments

can you do it without the singleton thing, e.g. if List is just a static property of Configuration?
@LouisRhys Maybe try something like that: stackoverflow.com/a/3862828/1073409
3

Yes, copy/paste/compile the following:

<ComboBox ItemsSource="Is it possible to Bind a ComboBox (WPF) ItemsSource to a read only string[]"/> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.