2013-05-04 69 views
0

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; 

有人能向我解释,这里是第一个场景问题的呢?

+1

我只是试图绑定两个'ListBoxes'到相同的'ObservableCollection'。为我工作。在输出窗口中查找绑定错误,如果它存在,它说什么?另外,如果你将它们都绑定到相同的集合,为什么你甚至需要在你的视图模型中有两个属性?只要将它们绑定到同一个属性即可。如果您发布视图模型的代码可能会有所帮助。 – 2013-05-04 04:14:37

+0

绑定没有错误。我认为我的问题是关于custorm usercontrol的ObserveableCollection。如果我更改为系统用户控件作为TextBlock,它的工作。感谢您的建议。我将编辑我的代码。 – 2013-05-04 04:20:29

+0

更多代码请(查看模型和视图)。另外,就像在WPF中,'UserControls'与'CustomControls'不同。我认为你对术语有点困惑。 – 2013-05-04 04:31:50

回答

1

我不知道你为什么使用这两个临时列表。您可以直接将项目添加到Observable集合中。试试这个:

foreach (MotionInfo info in listMotionInfo) 
{ 
     viewModel.RemoteItemsList.Add(info); 
     viewModel.LibraryItemsList.Add(info); 
    } 

下面,我试着为你创建解决方案。 我假设模型MotionTitleItem。

public class MotionTitleItem 
{ 
string _name = string.Empty; 

public string Name 
{ 
    get { return _name; } 
    set 
    { 
    _name = value; 
    OnPropertyChanged("Name"); 
    } 
} 


public event PropertyChangedEventHandler PropertyChanged; 

public void OnPropertyChanged(string propertyName) 
{ 
    try 
    { 
    PropertyChangedEventHandler eventHandler = this.PropertyChanged; 

    if (null == eventHandler) 
     return; 
    else 
    { 
     var e = new PropertyChangedEventArgs(propertyName); 
     eventHandler(this, e); 
    } 
    } 
    catch (Exception) 
    { 

    throw; 
    } 
} 

}

我认为这种应用模式是:

public class MotionTitleItemViewModel : INotifyPropertyChanged 
{ 
ObservableCollection<MotionTitleItem> _remoteItemsList = new ObservableCollection<MotionTitleItem>(); 

public ObservableCollection<MotionTitleItem> RemoteItemsList 
{ 
    get { return _remoteItemsList; } 
    set { _remoteItemsList = value; } 
} 

ObservableCollection<MotionTitleItem> _libraryItemsList = new ObservableCollection<MotionTitleItem>(); 

public ObservableCollection<MotionTitleItem> LibraryItemsList 
{ 
    get { return _libraryItemsList; } 
    set { _libraryItemsList = value; } 
} 

public MotionTitleItemViewModel() 
{ 
    MotionTitleItem motion; 
    for (int i = 0; i < 10; i++) 
    { 
    motion = new MotionTitleItem(); 
    motion.Name = "Name " + i.ToString(); 

    this.LibraryItemsList.Add(motion); 
    this.RemoteItemsList.Add(motion); 
    }  
} 

public event PropertyChangedEventHandler PropertyChanged; 

public void OnPropertyChanged(string propertyName) 
{ 
    try 
    { 
    PropertyChangedEventHandler eventHandler = this.PropertyChanged; 

    if (null == eventHandler) 
     return; 
    else 
    { 
     var e = new PropertyChangedEventArgs(propertyName); 
     eventHandler(this, e); 
    } 
    } 
    catch (Exception) 
    { 

    throw; 
    } 
} } 

我的看法是:

<Window x:Class="WPFExperiments.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
<ListBox x:Name="RemoteListBox" HorizontalAlignment="Right" Margin="0,0.5,8,0" 
     Width="382.5" 
     HorizontalContentAlignment="Stretch"     
     ItemsSource ="{Binding RemoteItemsList}"         
     SelectedIndex="0"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Name}"/> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
<ListBox x:Name="LibraryListBox" 
     Margin="4.5,0.5,437,0" 
     HorizontalContentAlignment="Stretch" 
     ItemsSource="{Binding LibraryItemsList}"     
     SelectedIndex="0"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding Name}"/> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
</Grid> 
</Window> 

在代码窗口我设置的DataContext查看模型的背后。

public partial class MainWindow : Window 
{ 
public MainWindow() 
{ 
    InitializeComponent(); 
    this.DataContext = new MotionTitleItemViewModel(); 
}} 

此代码适用于我。 以下是输出的截图。

enter image description here

投票,如果你觉得它有用这个答案。

玩得开心!

+0

我使用临时列表作其他用途,所以我使用此列表将其分配给2'ObserverableCollection'。如果我像你一样更改我的'ListBox.ItemPlate',它就会工作。但是我的'MotionTitleItem'用户控件无法将模板更改为文本块。无论如何,谢谢你的回答。 – 2013-05-06 03:02:11