[英]C# How do I extract Text from a DataGrid (Not DataGridView)?
I'm using a DataGrid (Not a DataGridView) to display some info, and once I press a, say a save button I want all the data to be moved to a listbox column wise... 我正在使用DataGrid(而不是DataGridView)显示一些信息,一旦按a,说一个保存按钮,我希望将所有数据明智地移动到列表框列中。
Help's appreciated guys! 帮忙,感激的家伙! :D
:D
EDIT: (here's the code so I want the data from the columns below to be read) I tried this : MessageBox.Show(SMLDataGrid.Items[0].ToString());
编辑:(这是代码,所以我想从下面的列中读取数据)我试过了:
MessageBox.Show(SMLDataGrid.Items[0].ToString());
There's no .Value
option either... or anything similar that I've seen 没有
.Value
选项...或我见过的任何类似东西
<DataGrid x:Name="SMLDataGrid" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"
materialDesign:DataGridAssist.CellPadding="13 8 8 8" materialDesign:DataGridAssist.ColumnHeaderPadding="8" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding BaseIn}"
Header="Base" x:Name="GrammarBaseColumn"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
<DataGridTextColumn Binding="{Binding PastIn}"
Header="Past Form" x:Name="GrammarPastColumn"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
<DataGridTextColumn Binding="{Binding PastPIn}"
Header="Past Participle Form" x:Name="GrammarPastPColumn"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
<DataGridTextColumn Binding="{Binding IesIn}"
Header="Plural Form" x:Name="GrammarIngColumn"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
<DataGridTextColumn Binding="{Binding IngIn}"
Header="Verb Form" x:Name="GrammarIesColumn"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>
</DataGrid.Columns>
</DataGrid>
EDIT:I've tried assigning the ItemSource of the DataGrid upon the Save Button press to another list as well through this: 编辑:我已经尝试通过单击“保存”按钮将DataGrid的ItemSource分配给另一个列表:
ListBox a = new ListBox();
a.ItemsSource = SMLDataGrid.Items;
MessageBox.Show(a.Items[0].ToString());
But the messagebox shows the name of the file + name of the class that holds all the strings.. 但是消息框显示文件名+包含所有字符串的类的名称。
You have probably set the ItemsSource property of the DataGrid to an IEnumerable<YourClass>
(if you haven't you should) where YourClass
is the class where the BaseIn, PastIn, PastPIn, ... properties are defined. 您可能已将DataGrid的ItemsSource属性设置为
IEnumerable<YourClass>
(如果尚未设置),其中YourClass
是定义BaseIn,PastIn,PastPIn等属性的类。
You can set the ItemsSource of the ListBox the same way: 您可以通过以下方式设置ListBox的ItemsSource:
listBox.ItemsSource = SMLDataGrid.ItemsSource;
var ic = SMLDataGrid.Items.OfType<YourClass>().ToList();
MessageBox.Show(ic[0].BaseIn);
The ListBox has no concept of columns though but depending on what you are trying to do you could use a ListView with a GridView instead: http://www.wpf-tutorial.com/listview-control/listview-with-gridview 尽管ListBox没有列的概念,但是根据您要执行的操作,您可以将ListView与GridView结合使用: http : //www.wpf-tutorial.com/listview-control/listview-with-gridview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.