名单

2016-08-22 53 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