2014-11-14 51 views
0

XAML綁定組合框一個ListView裏面一個的ObservableCollection <string>

<UserControl x:Class="PatientsInscrit_GMF.ListDisplay" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:custom="clr-namespace:PatientsInscrit_GMF" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="25"></RowDefinition> 
      <RowDefinition></RowDefinition> 
     </Grid.RowDefinitions> 
     <ListView Grid.Row="1" Margin="0,0,0,0" x:Name ="FileList"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Tag" Width="Auto"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <ComboBox ></ComboBox> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Grid> 
</UserControl> 

.CS

namespace PatientsInscrit_GMF 
{ 
    public partial class ListDisplay : UserControl 
    { 
     private ObservableCollection<string> _tags; 

     public ObservableCollection<string> Tags 
     { 
      get { return _tags; } 
     } 

     public ListDisplay() 
     { 
      InitializeComponent(); 
      _tags = new ObservableCollection<string>(); 
      List<string> tags = Facade.Instance.RetrieveTags(); 
      foreach(string s in tags) 
      { 
       _tags.Add(s); 
      } 
     } 
    } 
} 

如何綁定標籤到我的組合框,這是在我的列表視圖的列中的一個?

我試圖

ItemsSource = "{Binding Tags}" 

但組合框保留爲空。我知道門面列表檢索不是空的,因爲它創建時我添加了2個默認標籤,以便它不是空的。

我嘗試了各種其他的解決方案,我發現並沒有工作,甚至有代碼示例,甚至不會編譯。

請幫助:)

+0

不知道我關注你嗎? ItemsSource = {binding ...}那是綁定?正如我所說我嘗試了多種解決方案,我發佈的XAML是我需要添加綁定的乾淨狀態。你應該至少讀取所有的問題 – Fawar 2014-11-14 17:11:09

回答

1

我相信,在時間(最初)的Tags屬性是牽強,該_tags尚未初始化。 _tags初始化後,您可以提高Tags屬性更改(通過執行INotifyPropertyChanged)。然而,該屬性似乎只需要初始化(更改)一次。所以實現這樣的接口並不是必須的。你可以試試下面的代碼,以確保在Tags被取出時,數據將被初始化:

public ObservableCollection<string> Tags { 
    get { 
     if(_tags == null){ 
     //move your code after InitializeComponent() to here 
     _tags = new ObservableCollection<string>(); 
     List<string> tags = Facade.Instance.RetrieveTags(); 
     foreach(string s in tags) { 
      _tags.Add(s); 
     } 
     } 
     return _tags; 
    } 
} 

在XAML:

<ComboBox ItemsSource="{Binding Tags, 
      RelativeSource={RelativeSource AncestorType=UserControl}}"></ComboBox> 
+0

INotifyPropertyChanged默認在ObservableCollection(微軟類)中實現,所以我不必這樣做。我嘗試了你的解決方案,我的組合框裏面沒有任何東西。然而,我會保持該對象的懶惰實例化的想法:) – Fawar 2014-11-14 17:19:37

+0

@Fawar不知道爲什麼它不起作用。但是你誤解了我在這裏所說的。我不是指'ObservableCollection',我的意思是你的班級*** ListDisplay ***。你怎麼會這樣誤解? 「標籤」是該類的一個屬性,對嗎?提高'Tags'的屬性更改意味着您必須爲您的類實現INotifyPropertyChanged – 2014-11-14 17:21:11

+0

列表顯示是XAMl的CS,如果在XAML時沒有加載,那麼在WPF框架中存在大量問題;) – Fawar 2014-11-14 17:22:29

1

爲了數據綁定,請遵循以下幾點:

1)確保你已經將你的DataContext(在UserControl標籤中)設置爲DataContext="{Binding RelativeSource={RelativeSource Self}}"

2)你需要改變Item sSource在你的組合框結合:ItemsSource="{Binding SomePropertyName}"

3)你必須在襯紙CS文件

4來實現`INotifyPropertyChanged的」)要實現此屬性使用以下命令:

#region INotifyPorpertyChanged Memebers 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void NotifyPropertyChanged(string propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    #endregion 

5)製作您需要的對象類型的ObservableCollection類型的屬性。此屬性是ComboBoxis綁定的屬性。

例如:

private ObservableCollection<SomeDataType> _myPrivateData; 
public ObservableCollection<SomeDataType> SomePropertyName { get { return _myPrivateData; } set { _myPrivateData= value; NotifyPropertyChanged("SomePropertyName"); } } 

這需要照顧的數據綁定部分。現在,每次重置DataGrid綁定到的集合時,都會調用NotifyPropertyChanged來更新DataGrid。

+0

我試過了。它沒有奏效。另外,ObersableCollection 是一個微軟類,它將用於綁定過程中,因爲它實現了PropertyChangedEvent。所以我所要做的就是測試你的解決方案是添加DataContext。我仍然結束了一個空的組合框。 – Fawar 2014-11-14 17:16:27

+0

那麼你是否實現了INotifyPropertychanged? – 2014-11-14 17:22:54

+0

http://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx 只是去閱讀它,它已經在 – Fawar 2014-11-14 17:23:49

相關問題