Search code examples
wpfbindingcomboboxempty-list

wpf combobox binding is empty


I am trying to work out wpf with some difficulties. This ComboBox seems a very basic issue but I can't have it populated even after reading all possible similar post.

The extra difficulty I think is that the ComboBox is defined in a resource, here is the resource code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:s="clr-namespace:DiagramDesigner">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Styles/Shared.xaml"/>
    <ResourceDictionary Source="Styles/ToolBar.xaml"/>
</ResourceDictionary.MergedDictionaries>

<ToolBar x:Key="MyToolbar" Height="120">

    <!--Languages-->
    <GroupBox Header="Localization" Style="{StaticResource ToolbarGroup}" Margin="3">
        <Grid>
            <ComboBox Height="23" HorizontalAlignment="Center" 
                      VerticalAlignment="Top"  Width="120"
                      ItemsSource="{Binding _langListString}" 
                        DisplayMemberPath="ValueString" 
                        SelectedValuePath="ValueString" 
                        SelectedValue="{Binding LangString}"
                      />
        </Grid>
    </GroupBox>

  </ToolBar>

My data object is defined as follow:

public partial class Window1 : Window
{

    List<ComboBoxItemString> _langListString = new List<ComboBoxItemString>();

    // Object to bind the combobox selections to.
    private ViewModelString _viewModelString = new ViewModelString();


    public Window1()
    {
        // Localization settings
        _langListString.Add(new ComboBoxItemString()); _langListString[0].ValueString = "en-GB";
        _langListString.Add(new ComboBoxItemString()); _langListString[1].ValueString = "fr-FR";
        _langListString.Add(new ComboBoxItemString()); _langListString[2].ValueString = "en-US";


        // Set the data context for this window.
        DataContext = _viewModelString;


        InitializeComponent();


    }

And the modelview:

/// This class provides us with an object to fill a ComboBox with
/// that can be bound to string fields in the binding object.
public class ComboBoxItemString
{
    public string ValueString { get; set; }
}


//______________________________________________________________________
//______________________________________________________________________
//______________________________________________________________________



/// Class used to bind the combobox selections to. Must implement 
/// INotifyPropertyChanged in order to get the data binding to 
/// work correctly.
public class ViewModelString : INotifyPropertyChanged
{
    /// Need a void constructor in order to use as an object element 
    /// in the XAML.
    public ViewModelString()
    {
    }

    private string _langString = "en-GB";

    /// String property used in binding examples.
    public string LangString
    {
        get { return _langString; }
        set
        {
            if (_langString != value)
            {
                _langString = value;
                NotifyPropertyChanged("LangString");
            }
        }
    }

    #region INotifyPropertyChanged Members

    /// Need to implement this interface in order to get data binding
    /// to work properly.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

I just don't know what to try else. Is anyone has an idea of what is going on, and why the combobox stays empty?

Many thanks.


Solution

  • you can just bind to public properties

     ItemsSource="{Binding _langListString}"
    

    can not work because _langListString is not a public property