将List <string>绑定到GridViewColumn </string>

时间:2013-01-09 07:31:31

标签: wpf data-binding

我有一个GridView如下。

<Grid>
    <ListView Margin="8,44,8,50" TabIndex="57" Name="CustomFieldList">
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Header="Semester Name" Width="120" DisplayMemberBinding="{Binding FieldName}"/>
                <GridViewColumn Header="Subjects" Width="150" DisplayMemberBinding="{Binding TypeValidations}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>  

我的班级是;

  public class DigreePrograme
  {
      public string semester{ get; set; }
      public List<string> subjects { get; set; }
  }

我有一个观察者集合,我已将其绑定到列表中。

static ObservableCollection<DigreePrograme> digreeList = new   ObservableCollection<DigreePrograme>();
public ObservableCollection<DigreePrograme> DigreeList
{ 
    get { return digreeList }
    set { digreeList = value; OnPropertyChanged("DigreeList"); }  
}   
FormFoad

上的

CustomFieldList.ItemsSource=DigreeList;  

问题
semester属性显示完美。但是Subject列出没有。它显示为集合。如何解决这个问题?

2 个答案:

答案 0 :(得分:6)

您可以在ItemsControl的{​​{1}}中添加DataTemplate以显示所有GridViewColumn

XAML:

Subject

代码:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="275" Width="275" x:Name="UI">
    <Grid>
        <ListView Name="CustomFieldList" ItemsSource="{Binding ElementName=UI, Path=DigreeList}" >
            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="Semester Name" Width="120" DisplayMemberBinding="{Binding Semester}"/>
                    <GridViewColumn Header="Subjects" Width="150" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ItemsControl ItemsSource="{Binding Subjects}" Width="150" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

结果:

enter image description here

答案 1 :(得分:0)

我建议你在视图模型上创建一个新的字符串属性,它只会连接你的集合并返回它。这是最简单的方法..

  public class DigreePrograme
    {
        public string semester { get; set; }
        public List<string> subjects { get; set; }
        public string SubjectsDisplay
        {
            get
            {
                string returnVal = string.Empty;
                foreach (string subject in subjects)
                {
                    returnVal += subject + ", ";
                }
                return returnVal.TrimEnd(',', ' ');
            }
        }
    }

这样的事情可能有所帮助。

显然也会改变你的绑定:)

欢呼声。 STE。