1

I cant figure this one out. If I just have the combo-box by itself not embedded in a list box it will populate and selected the values I need.

The XAML

<ComboBox Name="comboBox1" 
          Height="23" 
          DataContext="{Binding Combox}" 
          ItemsSource="{Binding Comboxes}" 
          DisplayMemberPath="PV" 
          SelectedValuePath="PK"                  
          SelectedItem="{Binding SelectedItem}"
          VerticalAlignment="Top" 
          Width="120" />       

The Code Behind

    public MainWindow()
    {
        InitializeComponent();

        DataAttribute d = new DataAttribute(2, "blue");

        Combox c = new Combox();

        c.SelectedItem = d;
        c.Comboxes.Add(new DataAttribute(1, "red"));

        c.Comboxes.Add(new DataAttribute(3, "Black"));
        c.Comboxes.Add(c.SelectedItem);

        comboBox1.DataContext = c;
    }

Class for holding data

public class Combox: INotifyPropertyChanged
{
    public Combox()
    {
        Comboxes = new List<DataAttribute>();            
    }

    private DataAttribute _selectedItem;// = new DataAttribute(-1, "NA");

    public List<DataAttribute> Comboxes { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public DataAttribute SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            if (_selectedItem == value) return;
            _selectedItem = value;
            OnPropertyChanged("SelectedValue");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }        
}

public class DataAttribute
{
    public DataAttribute() { }

    public DataAttribute(int pk, string pv)
    {
        PK = pk;
        PV = pv;
    }

    public int PK { get; set; }
    public string PV { get; set; }

    public override string ToString()
    {
        return PV;
    }
}

All that works fine, but as soon as I try to create a list of com boxes in the listbox nothing. I can see the combo but no data. How on earth to you bind to it is XAML?

    public MainWindow()
    {
        InitializeComponent();

        List<Combox> com = new List<Combox>();
        DataAttribute d = new DataAttribute(2, "blue");

        Combox c = new Combox();

        c.SelectedItem = d;
        c.Comboxes.Add(new DataAttribute(1, "red"));

        c.Comboxes.Add(new DataAttribute(3, "Black"));
        c.Comboxes.Add(c.SelectedItem);

        com.Add(c);

        lstTest.ItemSource = com;
    }

As here is the XAML with a listbox. It no longer binds...

    <ListBox Name="lstTest" ItemsSource="{Binding}">        
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ComboBox Name="comboBox1" 
                      DataContext="{Binding Combox}" 
                      ItemsSource="{Binding Comboxes}" 
                      DisplayMemberPath="PV" 
                      SelectedValuePath="PK"                  
                      SelectedItem="{Binding SelectedItem}"
                      VerticalAlignment="Top" 
                      Width="120" />        
            </DataTemplate>
        </ListBox.ItemTemplate>            
    </ListBox>

I am stumped as it was hard enough trying to figure out how to just get the selected object to appear without the list-box...

1

1 Answer 1

1

Inside the items of an ItemsControl the DataContext is the currectly templated item, if you want to get to a list that is to be used as the ItemsSource for the ComboBoxes you normally need to modify the bindings to use another source, e.g. RelativeSource or ElementName. (This is the case for one list that is to be used for all ComboBoxes)

In this case, where the list seems to be part of the item, you should only need to remove the binding on the DataContext as the DataContext is already the item (an instance of Combox).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.