【问题标题】:binding ListBox to ObservableCollection in WPF在 WPF 中将 ListBox 绑定到 ObservableCollection
【发布时间】:2013-12-31 06:49:30
【问题描述】:

我有一个名为 Layer2Info 的类

    public class Layer2Info
{
    public ObservableCollection<totalAvailablePorts> availableClientPorts = new ObservableCollection<totalAvailablePorts>();
}

totalAvailablePorts 类是

    public class totalAvailablePorts : INotifyPropertyChanged
{
    public int _portID;
    public Boolean _isSelected;

    public int portID
    {
        get { return _portID; }
        set { _portID = value; NotifyPropertyChanged("portID"); }
    }
    public Boolean isSelected
    {
        get { return _isSelected; }
        set { _isSelected = value; NotifyPropertyChanged("isSelected"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public override string ToString()
    {
        return string.Format("{0}", portID);
    }
}

availableClientPorts中数据的创建是:

            for (int i = 1; i <= 3; i++)
        {
            totalAvailablePorts newPort = new totalAvailablePorts();
            newPort.portID = i;
            newPort.isSelected = false;
            layer2InfoConfig.availableClientPorts.Add(newPort);              
        }

现在,在我的 MainWindow 中,我将 ListBox 绑定到 Layer2Info.availableClientPorts,如下所示:

clientPortsList.ItemsSource = layer2InfoConfig.availableClientPorts;

最后是我的 xaml:

<ListBox x:Name="clientPortsList" 
         SelectionMode="Extended" 
         DisplayMemberPath="{Binding Path=portID}" 
         SelectedValuePath="{Binding Path=isSelected}" 
         Height="50"/>

现在,我可以看到 ListBox 中的所有端口 (1-3),但我想要做的是,在我在 ListBox 中选择的每一行上,我希望 availableClientPorts 中的 isSelected 值更改为true,我不知道从哪里开始。 有什么建议吗?

【问题讨论】:

    标签: c# wpf binding listbox observablecollection


    【解决方案1】:

    首先,SelectedValuePath 不是你想的那样。 MSDN says it "获取或设置用于从 SelectedItem 获取 SelectedValue 的路径。"因此,当用户选择一个项目时,clientPortsList 将获取它自己的SelectedItem 的该属性,并从clientPortsList.SelectedValue 返回该属性的值。对于多选,这对您来说不是一个真正有用的概念,无论如何它与您在这里提出的问题无关。

    您要做的是,对于每个totalAvailablePorts 实例,将该实例的isSelected 属性绑定到拥有它的ListBoxItemIsSelected 属性。您可以使用项目模板来做到这一点,但样式要简单得多(如果您对重新创建或更改默认的 ListBoxItem 视觉行为不感兴趣,那就更好了)。答案是already on StackOverflow

    <ListBox ItemsSource="..."
        x:Name="clientPortsList" 
        SelectionMode="Extended" 
        DisplayMemberPath="{Binding Path=portID}" >
      <ListBox.ItemContainerStyle>
       <Style TargetType="{x:Type ListBoxItem}">
         <!-- binding totalAvailablePorts.isSelected to ListBoxItem.IsSelected -->
         <Setter Property="IsSelected" Value="{Binding isSelected}"/>
       </Style>
      </ListBox.ItemContainerStyle>
     </ListBox>
    

    对于ListBoxItem 实例,它们的DataContext 将是它们各自的totalAvailablePorts 实例,因此isSelected(小写I)将“在范围内”。

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2012-12-16
      • 2018-09-11
      相关资源
      最近更新 更多