문제

I am having trouble adding a row that displays all the values of the member of an object.

Here is how I have set up my listview:

    <ListView Height="178" HorizontalAlignment="Left" Margin="238,31,0,0" Name="SpoolSheetListView" VerticalAlignment="Top" Width="555" HorizontalContentAlignment="Stretch" SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="85"  Header="Column 1" />
                <GridViewColumn Width="120" Header="Column 2" />
                <GridViewColumn Width="120" Header="Column 3" />
                <GridViewColumn Width="120" Header="Column 4" />
                <GridViewColumn Width="115" Header="Column 5" />
            </GridView>
        </ListView.View>
    </ListView>

say, myObject has 5 members: member1 to member5, which are all of type string. How do I add a single row in the ListView for that object?

Thanks again people!

도움이 되었습니까?

해결책

WPF list controls work best when you use them with data binding; you should bind the ItemsSource of the ListView to a list of objects, and bind each column to a property of these objects.

<ListView ItemsSource="{Binding Items}" Height="178" HorizontalAlignment="Left" Margin="238,31,0,0" Name="SpoolSheetListView" VerticalAlignment="Top" Width="555" HorizontalContentAlignment="Stretch" SelectionMode="Single">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="85"  Header="Column 1" DisplayMemberBinding="{Binding Member1}" />
            <GridViewColumn Width="120" Header="Column 2" DisplayMemberBinding="{Binding Member2}" />
            <GridViewColumn Width="120" Header="Column 3" DisplayMemberBinding="{Binding Member3}" />
            <GridViewColumn Width="120" Header="Column 4" DisplayMemberBinding="{Binding Member4}" />
            <GridViewColumn Width="115" Header="Column 5" DisplayMemberBinding="{Binding Member5}" />
        </GridView>
    </ListView.View>
</ListView>

(Items being a property of the DataContext that returns a collection of objects)

To add a row to the ListView, you just need to add an item to the Items collection (note that the collection should implement INotifyCollectionChanged so that the ListView is notified; the ObservableCollection<T> class works fine for most cases)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top