0

我试图在通过 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; }
}
4

2 回答 2

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();
于 2013-07-29T03:01:37.053 回答
0

嗨,如果您使用的是 MVVM,那么您可以绑定 ListBox 的 SelectedItem,例如

xml

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

}

自定义类型

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 的引用必须与 Collection 中绑定到 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" />
于 2013-07-29T02:31:17.690 回答