【发布时间】: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