2013-05-04 64 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