New Developer
New Developer

Reputation: 3305

bind List<string> to GridViewColumn

I have a GridView like following.

<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

CustomFieldList.ItemsSource=DigreeList;  

Issue
semester property displays perfectly. But Subject List not. It displys as Collection. How to fix this?

Upvotes: 3

Views: 5869

Answers (2)

sa_ddam213
sa_ddam213

Reputation: 43596

You could add a ItemsControl in a DataTemplate in your GridViewColumn to show all the Subject items

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>

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

Upvotes: 7

Steoates
Steoates

Reputation: 3168

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.

Upvotes: 0

Related Questions