Question

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.

Was it helpful?

Solution

you can just bind to public properties

 ItemsSource="{Binding _langListString}"

can not work because _langListString is not a public property

OTHER TIPS

By my analysis the problem consist in your DataContext.

DataContext = _viewModelString;

If you give the viewModelString to the DataContext you have to have the _langListString >defined there, in order to the combobox know which item it is bound to.

This is what I would do:

  1. Add List _langListString = new List(); to the >ModelView.
  2. _langListString would be _viewModelString._langListString.add(Your Items) - be >carefull to instatiate the _langList when you create your _viewModelString object.

Then I think the rest would work.


Many thanks, I have the changes you've suggested but this combobox still stays empty :-(

The new modelview looks like this:

/// 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
{
    public List<ComboBoxItemString> _langListString {get;set;}
    /// Need a void constructor in order to use as an object element 
    /// in the XAML.
    public ViewModelString()
    {
        // Localization settings
        _langListString = new List<ComboBoxItemString>();
        ComboBoxItemString c;
        c = new ComboBoxItemString(); c.ValueString = "en-GB"; _langListString.Add(c);
        c = new ComboBoxItemString(); c.ValueString = "fr-FR"; _langListString.Add(c);
        c = new ComboBoxItemString(); c.ValueString = "en-US"; _langListString.Add(c); 
    }

    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
}

The data object:

  // Object to bind the combobox selections to.
    private ViewModelString _viewModelString;

    public Window1()
    {
        // Set the data context for this window.
        _viewModelString = new ViewModelString();
        DataContext = _viewModelString;

        InitializeComponent();
    }

And I have tried all possible combination in the combobox (_langListString, _viewModelString._langListString, _viewModelString) it just doesn't work:

<ComboBox Height="23" HorizontalAlignment="Center" 
                      VerticalAlignment="Top"  Width="120"
                      ItemsSource="{Binding _langListString}" 
                        DisplayMemberPath="ValueString" 
                        SelectedValuePath="ValueString" 
                        SelectedValue="{Binding LangString}"
                      />

I tend to think that this xaml is making things really complicated without possibility of debugging. Is anyone can help???

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top