我有一個名爲Layer2Info綁定列表框的ObservableCollection在WPF
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);
}
現在,在我的主窗口中,我將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,我不知道從哪裏開始。 有什麼建議嗎?
優秀的解決方案,像魅力一樣工作! – amirm