将多个列表框项目绑定到1个UserControl的ObserverableCollection

时间:2013-05-04 03:59:42

标签: c# wpf

我在下面有两个列表框:

<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>

我的viewmodel

    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");
        }
    }

我将两个ListBox 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是一个自定义用户控件。 我的问题是只有第一个ListBox与ItemSource绑定与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 个答案:

答案 0 :(得分: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

如果您觉得有用,请投票给我。

玩得开心!