我有ObservableCollection和WPF列表框相互綁定。 我想在添加ObservableCollection時同時在Listbox上顯示芯片位置。ObservableCollection和WPF列表框綁定
<Window x:Class="Apeiron.ZoneSetter.ZoneSetterWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="765" Width="765">
<Grid>
<ListBox Name="lbPosList" ItemsSource="{Binding }" ></ListBox>
</Grid>
</Window>
案例1)它運作良好。當ObservableCollection被改變時,它在ListBox顯示chnaged芯片pos。
public partial class ZoneSetterWindow : Window, INotifyPropertyChanged
{
public ZoneSetterWindow()
{
InitializeComponent();
lbChipList.DataContext = SelectedZoneChipList;
}
public void AddChip(ZoneMapChipInfo chip)
{
if (!m_SelectedChipDic.ContainsKey(chip.Point))
{
m_SelectedChipDic.Add(chip.ChipPos, chip);
m_selectedzonechiplist.Add(chip);
}
}
private Dictionary<Point, ZoneMapChipInfo> m_SelectedChipDic = new Dictionary<Point, ZoneMapChipInfo>();
private ObservableCollection<ZoneMapChipInfo> m_selectedzonechiplist = new ObservableCollection<ZoneMapChipInfo>();
public ObservableCollection<ZoneMapChipInfo> SelectedZoneChipList
{
get
{
return m_selectedzonechiplist;
}
}
}
案例2)它不起作用。儘管ObservableCollection已更改,但它不會在ListBox中顯示chnaged芯片pos。
public partial class ZoneSetterWindow : Window, INotifyPropertyChanged
{
public ZoneSetterWindow()
{
InitializeComponent();
lbChipList.DataContext = SelectedZoneChipList;
}
public void AddChip(ZoneMapChipInfo chip)
{
if (!m_SelectedChipDic.ContainsKey(chip.Point))
{
m_SelectedChipDic.Add(chip.ChipPos, chip);
m_selectedzonechiplist.Add(chip);
}
}
private Dictionary<Point, ZoneMapChipInfo> m_SelectedChipDic = new Dictionary<Point, ZoneMapChipInfo>();
private ObservableCollection<ZoneMapChipInfo> m_selectedzonechiplist = new ObservableCollection<ZoneMapChipInfo>();
public ObservableCollection<ZoneMapChipInfo> SelectedZoneChipList
{
get
{
ObservableCollection<WaferZoneMapChipInfo> result = new ObservableCollection<WaferZoneMapChipInfo>();
foreach (ZoneMapChipInfo info in m_SelectedChipDic.Values)
{
result.Add(info);
}
return result;
}
}
}
我要同步m_SelectedChipDic和SelectedZoneChipList總是 並在列表框中顯示SelectedZoneChipList的ChipPosition。
我不知道以上兩種情況有什麼不同。
是否有人瞭解我有關上述問題。謝謝!!
非常感謝!你的答案有助於我理解ObservableCollection。我會試試看。 ObservableDictionary!順便說一下,ObservableDictionary的哪一項被綁定?關鍵或價值? – Jini
我會研究你的網頁的內容。謝謝! – Jini