1 ObserverableCollection我有两个列表框定义如下:绑定多列表框itemsouce与用户控件
<ListBox x:Name="RemoteListBox" HorizontalAlignment="Right" Margin="0,88.5,8,0"
Width="382.5"
HorizontalContentAlignment="Stretch"
ItemsSource ="{Binding RemoteItemsList}"
SelectedIndex="0">
</ListBox>
<ListBox x:Name="LibraryListBox"
Margin="4.5,88.5,437,0"
HorizontalContentAlignment="Stretch"
ItemsSource="{Binding LibraryItemsList}"
SelectedIndex="0">
</ListBox>
我的视图模型
private ObservableCollection<MotionTitleItem> _remoteItemsList;
public ObservableCollection<MotionTitleItem> RemoteItemsList
{
get { return _remoteItemsList; }
set
{
_remoteItemsList = value;
NotifyPropertyChanged("RemoteItemsList");
}
}
private ObservableCollection<MotionTitleItem> _libraryItemsList
public ObservableCollection<MotionTitleItem> LibraryItemsList
{
get { return _libraryItemsList; }
set
{
_libraryItemsList = value;
NotifyPropertyChanged("LibraryItemsList");
}
}
我结合两个列表框的ItemSource与ObserverableCollection以下定义:
var listMotion = new ObservableCollection<MotionTitleItem>();
foreach (MotionInfo info in listMotionInfo)
{
var motionTitleItem = new MotionTitleItem();
listMotion.Add(motionTitleItem);
}
viewModel.RemoteItemsList = listMotion;
viewModel.LibraryItemsList = listMotion;
MotionTitleItem是一个自定义用户控件。 我的问题是只有第一个与ItemSource绑定的ListBox与RemoteListItem显示在UI中的项目,另一个不是。 如果我绑定两个ListBox中的ItemSource 2 ObserverableCollection,问题解决了:
var listMotion = new ObservableCollection<MotionTitleItem>();
var listMotion2 = new ObservableCollection<MotionTitleItem>();
foreach (MotionInfo info in listMotionInfo)
{
var motionTitleItem = new MotionTitleItem();
listMotion.Add(motionTitleItem);
var motionTitleItem2 = new MotionTitleItem();
listMotion2.Add(motionTitleItem2);
}
viewModel.RemoteItemsList = listMotion;
viewModel.LibraryItemsList = listMotion2;
有人能向我解释,这里是第一个场景问题的呢?
我只是试图绑定两个'ListBoxes'到相同的'ObservableCollection'。为我工作。在输出窗口中查找绑定错误,如果它存在,它说什么?另外,如果你将它们都绑定到相同的集合,为什么你甚至需要在你的视图模型中有两个属性?只要将它们绑定到同一个属性即可。如果您发布视图模型的代码可能会有所帮助。 – 2013-05-04 04:14:37
绑定没有错误。我认为我的问题是关于custorm usercontrol的ObserveableCollection。如果我更改为系统用户控件作为TextBlock,它的工作。感谢您的建议。我将编辑我的代码。 – 2013-05-04 04:20:29
更多代码请(查看模型和视图)。另外,就像在WPF中,'UserControls'与'CustomControls'不同。我认为你对术语有点困惑。 – 2013-05-04 04:31:50