Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 访问值的多列列表框_C#_Wpf_Listbox - Fatal编程技术网

C# 访问值的多列列表框

C# 访问值的多列列表框,c#,wpf,listbox,C#,Wpf,Listbox,可能重复: 我将listview定义为: <ListView Height="234.522" Name="chartListView" Width="260" SelectionChanged="chartListView_SelectionChanged" HorizontalAlignment="Left" Margin="251,38,0,38"> <ListView.View> <GridView>

可能重复:

我将listview定义为:

<ListView Height="234.522" Name="chartListView" Width="260" SelectionChanged="chartListView_SelectionChanged" HorizontalAlignment="Left" Margin="251,38,0,38">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="op1" DisplayMemberBinding="{Binding op1}" Width="50"/>
                <GridViewColumn Header="op2" DisplayMemberBinding="{Binding op2}" Width="50"/>
                <GridViewColumn Header="op3" DisplayMemberBinding="{Binding op3}" Width="50"/>
                <GridViewColumn Header="op4" DisplayMemberBinding="{Binding op4}" Width="50"/>
                <GridViewColumn Header="op5." DisplayMemberBinding="{Binding op5}" Width="50"/>
            </GridView>
        </ListView.View>
    </ListView>
如何访问所选项目之一,以便查看列的值(例如,
op3
)?下面的代码显示了所有的值,但我只想看到
op3

MessageBox.Show(chartListView.SelectedItem.ToString());

我建议使用MVVM模式将UI与底层逻辑连接起来

在互联网上,你会发现大量关于M*VVM模式的信息和资源*

其核心思想是拥有一个包含数据结构和变量的视图模型(VM)。这些数据结构和变量是完全自治的,不以任何方式依赖于UI层。从VM到UI/视图的结构和变量的连接存在于数据绑定中

因此,您要做的(很短的时间内)是将列表控件的ItemSource属性与一些底层数据结构连接起来(这里通常的结构是一个ObservableCollection-阅读文献以了解原因)

这是第一步。第二步可能是将object类型的变量添加到ViewModel中,该变量将保存列表中当前选定的项。要实现此功能,只需将列表的SelectedItem-属性与此变量连接即可

从那时起,每当您在列表中选择一个项目时,视图中的变量将保留其值


当然,这是非常短期的,而且相当粗略,但也许它给了您深入研究MVVM模式的想法——这是经过深思熟虑的;)

看一看:谢谢这给了我我一直在寻找的:)谢谢Marc我将研究MVVM。。。
MessageBox.Show(chartListView.SelectedItem.ToString());