简体   繁体   English

ListView ItemsSource绑定

[英]ListView ItemsSource binding

I have a ListView with 3 columns: 我有3列的ListView:

<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. 当前,我已将ParamName设置为我的ItemsSource,显然,这种方式将无法实现所需的结果。

In my ViewModel, I have ObservableCollections for each Column ( ParamName for Name, ParamType for Type and ParamContent for Content). 在我的ViewModel,我已经为每列ObservableCollections( ParamName的名称, ParamType的类型和ParamContent的内容)。 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. 这些ObservableCollections已正确填充,并且我能够通过Binding接收它们的数据,但无法用各自的数据填充列。

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. 其中MyRow是具有所需属性的类或结构。

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? 如果您已经有了这些Collections-请尝试将其连接到一个主要Collections上,并告诉GridColumns您希望将哪些属性绑定到每个Column,但是我不确定ObservableCollections是否可行-如果它们的长度不同怎么办?

And you can still create your own Collection from these three - just parse it... 而且您仍然可以从这三个中创建自己的收藏集-只需对其进行解析...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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