0
votes

I am trying to set some of the ListBox elements as selected when binding through DataContext. ListBox is binded through code behind.

I am binding my listbox on user control's constructor

TradesListBox.ItemsSource = config.OfType<Trade>().ToList();

XAML below is a part of UserControl whose DisplayMemberPath property is being set through a constructor as shown in line above, while I am trying to set SelectedItem property from DataContext that is being passed through owing window. But SelectedItem are not being displayed

<Label Grid.Row="1" Grid.Column="0" Target="{Binding ElementName=TradesListBox}" Style="{StaticResource LabelStyle}" FontSize="18" HorizontalAlignment="Right">_Trades</Label>
<ListBox Grid.Row="1" Grid.Column="1" Name="TradesListBox"  HorizontalAlignment="Stretch" Height="70" Margin="2" DisplayMemberPath="ConfigValue" SelectedItem="{Binding Trade.ConfigValue}" SelectionMode="Multiple" />

private List<Trade> trade;
[DataMember]
public virtual List<Trade> Trade
{
    get
    {
        if (trade == null)
            trade = new List<Trade>();
        return trade; 
    }
    set
    { trade = value == null ? new List<Trade>() : value; }
}
2
Post some relevant code and XAML. - Federico Berasategui
What is the selected value being bound to? Also the list box needs to be synched up - Gayot Fow
Is the data context here shared with the same data context here? stackoverflow.com/q/17899868/1042344 - Gayot Fow

2 Answers

0
votes

I think items are selected but not highlighted, if so then try to set Focus on ListBox and see if it works for you.

TradesListBox.ItemsSource = config.OfType<Trade>().ToList();

// Let say you want to Select first and second item
TradesListBox.SelectedItems.Add(TradesListBox.Items[0]);
TradesListBox.SelectedItems.Add(TradesListBox.Items[1]);

// Set focus on ListBox
TradesListBox.Focus();
0
votes

Hi If you are using MVVM then you can bind SelectedItem of ListBox like

xaml

<ListBox ItemsSource="{Binding Students}" SelectedItem="{Binding SelectedStudent}" DisplayMemberPath="Name"></ListBox>

.cs

    public partial class MainWindow : Window
{
    public MainWindow()
    {
       InitializeComponent();
       DataContext = new ViewModel();
    }
}

ViewModel

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Students = new ObservableCollection<Student>();
        Students.Add(new Student { Name = "abc", Age = 20 });
        Students.Add(new Student { Name = "pqr", Age = 30 });
        Students.Add(new Student { Name = "xyz", Age = 40 });
        SelectedStudent = Students[0];
    }

    public ObservableCollection<Student> Students { get; set; }

    Student selectedStudent;
    public Student SelectedStudent
    {
        get { return selectedStudent; }
        set { selectedStudent = value; Notify("SelectedStudent"); }
    }


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

    public event PropertyChangedEventHandler PropertyChanged;

}

CustomType

public class Student:INotifyPropertyChanged
{
    string name;
    public string Name {
        get
        { return name; }

        set
        {
            name = value;
            Notify("Name");
        }
    }


    int age;

    public int Age
    {
        get
        { return age; }

        set
        {
            age = value;
            Notify("Age");
        }
    }


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

    public event PropertyChangedEventHandler PropertyChanged;
}

Remember the reference of SelectedItem must be same as that one of the item in Collection binded to ItemsSource >Edit

<ListBox Grid.Row="1" Grid.Column="1" Name="TradesListBox"  HorizontalAlignment="Stretch" 
Height="70" Margin="2" DisplayMemberPath="ConfigValue" 
SelectedValue="{Binding Trade.ConfigValue}" 
SelectedValuePath="ConfigValue" SelectionMode="Multiple" />