简体   繁体   中英

ListView ItemsSource binding

I have a ListView with 3 columns:

<ListView ItemsSource="{Binding ParamName}" HorizontalAlignment="Left" Height="109" Margin="10,87,0,0" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="281">
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </GridView.ColumnHeaderContainerStyle>
            <GridViewColumn Header="Name" Width="60"/>
            <GridViewColumn Header="Type" Width="60"/>
            <GridViewColumn Header="Content" Width="156"/>
        </GridView>
    </ListView.View>
</ListView>

Currently, I have set ParamName as my ItemsSource, which will obviously not achieve the desired result this way.

In my ViewModel, I have ObservableCollections for each Column ( ParamName for Name, ParamType for Type and ParamContent for Content). Those ObservableCollections are correctly filled and I am able to receive their data through the Binding, but I cannot fill the columns with their respective data.

I have thought of certain possible solutions, but none of them seem to work. What would be the best approach for this problem?

Here's how it looks like (left) and how it should look like (right):

怎么样 应该如何

Naming them after their Types might be a little bit confusing.

You have three Collections, but only one is binded. So all columns have same value. Try to create one Collection containing all you need:

ObservableCollection<MyRow>

where MyRow is a Class or Struct with Properties you need.

If you already have these Collections - try to concatenate it to one major Collection and tell the GridColumns which Properties you wish to bind to each Column, but I'm not sure if it's possible with ObservableCollections - what if they have different length?

And you can still create your own Collection from these three - just parse it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM