How to use DisplayMemberPath to show an item in a specific column
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开发者_如何学Python information like assignment.Title or assignment.Due?
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>
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论