wpf 将 List<string> 绑定到 GridViewColumn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14230300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:58:17  来源:igfitidea点击:

bind List<string> to GridViewColumn

wpfdata-binding

提问by New Developer

I have a GridViewlike following.

我有一个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>  

My class is;

我的班级是;

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

I have an observer collection and I have bound that to list.

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

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

on the FormFoad

FormFood 上

CustomFieldList.ItemsSource=DigreeList;  

Issue
semesterproperty displays perfectly. But SubjectList not. It displys as Collection. How to fix this?

问题
semester属性显示完美。但Subject列表不是。它显示为Collection。如何解决这个问题?

回答by sa_ddam213

You could add a ItemsControlin a DataTemplatein your GridViewColumnto show all the Subjectitems

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

xaml:

xml:

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

Code:

代码:

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

result:

结果:

enter image description here

在此处输入图片说明

回答by Steoates

I would suggest you create a new string property on your view model that would just concatenate your collection and return that. It would be the simplest way to go about this..

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

  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(',', ' ');
            }
        }
    }

something like that may help.

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

obviously change your binding also :)

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

cheers. ste.

干杯。圣。