名單

2016-08-22 48 views
0

這裏是我的問題: 我已經得到了這些類:名單

public class CsvField 
{ 
    public string Content { get; set; } 

    public CsvField(string content) 
    { 
     Content = content; 
    } 

} 

public class CsvLine 
{ 
    public List<CsvField> Fields = new List<CsvField>(); 

    public int LineNumber; 

} 

public static class Settings 
{ 
    public static List<string> Tags = new List<string>(); 
    public static CsvLine AllHeaders = new CsvLine(); 
} 

我想要做的,是顯示包含Settings.AllHeaders.Fields的每一個成員,並含有組合框列表框Settings.Tags列表中的所有成員(水平放置 - 左邊是AllHeaders的成員,旁邊是組合框)。所以,如果我有4個標題,我會得到這4個標題和4個組合框的列表,每個標題旁邊有個別標題。這些組合框中的每一個都會包含一個標籤列表。

所以,我定義一個DataTemplate:

<Window x:Class="CSV_To_Tags_App.Window2" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:loc="clr-namespace:CSV_To_Tags_App" 
     Title="Window2" Height="435" Width="566"> 

    <Window.Resources> 
     <DataTemplate DataType="{x:Type loc:CsvField}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock x:Name="HeaderTextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" 
         VerticalAlignment="Top" Text="{Binding Content}" 
       /> 
       <ComboBox HorizontalAlignment="Right" VerticalAlignment="Top" Width="120"/> 

      </StackPanel> 
     </DataTemplate>      
    </Window.Resources> 


    <Grid> 

     <Label Content="Available headers" HorizontalAlignment="Left" 
       VerticalAlignment="Top"/> 
     <ListBox x:Name="HeadersListtListBox" HorizontalAlignment="Left" 
       Height="254" Margin="36,104,0,0" VerticalAlignment="Top" Width="452" 
       ItemsSource="{Binding}"/>  
    </Grid> 
</Window> 

現在,XAML代碼上面是不完整的,因爲我不知道如何: 1.綁定的TextBlock到Settings.AllHeaders.Fields.Content
2.將ComboBox綁定到標籤列表

+0

使用中繼器http://stackoverflow.com/questions/3010131/wpf-repeater-like-control-for-collection-source –

+0

不幸的是,我不明白怎麼例子可以幫助我。 – Loreno

回答

0

@ MD's提供的鏈接確實爲您提出的問題提供瞭解決方案,但您必須重新排列一些東西才能看到它。 下面的XAML會給你你所要求的,假設你綁定的類正確設置了數據綁定(實現INotifyPropertyChanged接口)。這個網站上有很多關於如何正確實施該部分的例子。

<Window x:Class="CSV_To_Tags_App.Window2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:loc="clr-namespace:CSV_To_Tags_App" 
    Title="Window2" Height="435" Width="566"> 
<Grid> 
    <StackPanel Orientation="Horizontal"> 
    <ItemsControl ItemsSource="{Binding Path=Settings.AllHeadings.Fields}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Path=Content}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    <ItemsControl ItemsSource="{Binding Path=Settings}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <ComboBox ItemsSource="{Binding Path=Tags}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    </StackPanel> 
</Grid> 
</Window> 
+0

但是如果我的設置類是靜態的呢?我無法實現它的接口。 – Loreno

+0

我真的需要那個INotifyChanged接口嗎?我只想顯示數據而沒有修改它的選項。 – Loreno

+0

哦,我沒有看到你的Settings類是靜態的。你不能對此進行數據綁定。數據綁定需要你有一個綁定對象,意味着一個實例化對象,這與靜態類完全相反。另一方面:INotifyPropertyChanged不是爲了從UI修改數據,而是用來在UI中顯示綁定數據。你可以在沒有它的情況下做一個有限的數據綁定形式,但是如果沒有實現INotifyPropertyChanged,那麼在UI中不會顯示任何代碼中綁定對象的改變(這就是你正在做的)。 – Stewbob