views:

215

answers:

1

I have a ListView with several GridViewColumns (Title, Start, Due). So how can I bind an object with a string Title, datetime Start, and datetime Due. In procedural code I have already stated:

lvwFill.ItemsSource = assignments.ListAssignments(); //Returns a List<Assignments>

So now my XAML is:

            <ListView x:Name="lvwFill">
            <ListView.View>
                <GridView>
                    <GridViewColumn x:Name="TitleColumn" Header="Title"  Width="125" />
                    <GridViewColumn x:Name="DueColumn" Header="Due"  Width="75" />
                    <!-- ... -->
                </GridView>
            </ListView.View>
        </ListView>

So how can I list each assignment and ONLY show certain information like assignment.Title or assignment.Due?

+1  A: 

use the DisplayMemberBinding like so

          <GridViewColumn
             Header="Title"
             DisplayMemberBinding="{Binding Path=Title}" />

          <GridViewColumn
             Header="Due"
             DisplayMemberBinding="{Binding Path=Due}" />

assuming you have bound to a list of Assignments that have a Title and Due property.

if you wish to have complex value types (not strings) use the CellTemplate, it allows you to use a data template so you can format/convert to your hearts content

        <GridViewColumn
            Header="Due">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Due, StringFormat={}{0:MM/dd/yyyy}}" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
Aran Mulholland
Can I also format it? Like I have an int Kind that represents an enum. I tried to display the values of enum Kind and it passed it as an int. Do I need a converter or something?
Mohit Deshpande
yep you can format it using a cell template
Aran Mulholland