wpf - 将 List<string> 绑定(bind)到 GridViewColumn

标签 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; }
  }

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

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 列表没有。它显示为 Collection。如何解决这个问题?

最佳答案

您可以在 GridViewColumnDataTemplate 中添加一个 ItemsControl 以显示所有 Subject 项目

xaml:

<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>

代码:

public partial class MainWindow : Window 
{
    public MainWindow()
    {
        InitializeComponent();
        DigreeList.Add(new DigreePrograme { Semester = "Semester 1", Subjects = new List<string>(new string[] {"Math","Art","Engish" }) });
    }

    private ObservableCollection<DigreePrograme> digreeList = new ObservableCollection<DigreePrograme>();
    public ObservableCollection<DigreePrograme> DigreeList
    {
        get { return digreeList; }
        set { digreeList = value;}
    }   
}

public class DigreePrograme
{
    public string Semester { get; set; }
    public List<string> Subjects { get; set; }
}

结果:

enter image description here

关于wpf - 将 List<string> 绑定(bind)到 GridViewColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230300/

相关文章:

Android 数据绑定(bind) Activity 结束()

wpf - 一次更新 UserControl 中的所有绑定(bind)

wpf - 使用 ObservableCollection 数据绑定(bind)从线程更新数据网格

c# - WINFORM 或 WPF : How to trigger custom event inside the constructor of the class that emits it

c# - 如何在 DateTimePicker 中禁用键盘输入?

c# - Caliburn Micro WPF : Message. Attach with guard 属性禁用整个主机控制

javascript - SAPUI5中数据绑定(bind)是如何实现的?

c# - WPF locbaml-ed 应用程序和运行时语言切换?

.net - 桌面应用程序中的流体布局包装面板/VariableSizedWrapGrid?

javascript - Knockout.js 在 foreach 绑定(bind)中绑定(bind)选择选项和值