0

我在下面定义了两个列表框:

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

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

有人可以向我解释第一个场景问题的重点在哪里吗?

4

1 回答 1

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

这段代码对我有用。这是输出的屏幕截图。

在此处输入图像描述

如果您觉得这个答案有用,请投票给它。

玩得开心!

于 2013-05-04T13:19:23.627 回答