当ListBox从后面的代码绑定时,如何使列表框元素从DataContext中选择

时间:2013-07-29 02:06:56

标签: wpf data-binding

我试图在通过DataContext绑定时将一些ListBox元素设置为选中。 ListBox通过代码隐藏。

我在用户控件的构造函数

上绑定了我的列表框
TradesListBox.ItemsSource = config.OfType<Trade>().ToList();

下面的XAML是UserControl的一部分,其DisplayMemberPath属性是通过构造函数设置的,如上面的行所示,而我正在尝试从正在通过窗口传递的DataContext中设置SelectedItem属性。但是没有显示SelectedItem

<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 个答案:

答案 0 :(得分:0)

您好如果您正在使用MVVM,那么您可以绑定ListBox的SelectedItem,如

  

XAML

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

的.cs

    public partial class MainWindow : Window
{
    public MainWindow()
    {
       InitializeComponent();
       DataContext = new 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;
}

请记住,SelectedItem的引用必须与绑定到ItemsSource的集合中的项目相同&gt;编辑

<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" />

答案 1 :(得分:0)

我认为项目已被选中但未突出显示,如果是,请尝试将焦点设置在ListBox上,看看它是否适合您。

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();