DataGrid不使用C#在WPF中显示数据 [英] Datagrid not showing data in wpf with c#
问题描述
我正在尝试使用C#在WPF中使用dataGrid.但是,当我在调试模式下运行程序时,我无法使datagrid在表中显示任何数据.当数据网格加载时,我将执行此代码.但是我所看到的只是一个空的正方形.
I am trying to use a dataGrid in WPF with c#. But I cannot get my datagrid to show any of my data in the table when I run my program in debug mode. I have this code executing when the datagrid loads. But all I see is an empty square.
private void dataGrid1_Loaded(object sender, RoutedEventArgs e)
{
var items = new List<SaveTable>();
items.Add(new SaveTable("A" , 0));
items.Add(new SaveTable("B" , 0));
items.Add(new SaveTable("C" , 0));
items.Add(new SaveTable("D" , 0));
items.Add(new SaveTable("E" , 0));
var grid = sender as DataGrid;
grid.ItemsSource = items;
}
我保存了一个名为SaveTable的类,如下所示:
I save a class named SaveTable which looks like this:
class SaveTable
{
public string Name { get; set; }
public double Value { get; set; }
public SaveTable(string name, double value)
{
this.Name = name;
this.Value = value;
}
}
我在线获得了此代码格式,看来一切都正确吗?有什么建议吗?
I got this code format online and it seems like everything is right? any suggestions?
这是该窗口的xaml代码
here is the xaml code for that window
<Window x:Class="RobustCalculator.Storage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Storage" Height="499" Width="546" Activated="Window_Activated" Loaded="Window_Loaded">
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="A" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="24,10,0,306" Name="valueA" Width="120" TextChanged="valueA_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,39,0,0" Name="valueB" VerticalAlignment="Top" Width="120" TextChanged="valueB_TextChanged" />
<TextBlock Height="20" HorizontalAlignment="Left" Margin="10,42,0,0" Name="textBlock2" Text="B" VerticalAlignment="Top" Width="20" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,71,0,0" Name="valueC" VerticalAlignment="Top" Width="120" TextChanged="valueC_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,72,0,0" Name="textBlock3" Text="C" VerticalAlignment="Top" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="10,101,0,0" Name="textBlock4" Text="D" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,98,0,0" Name="valueD" VerticalAlignment="Top" Width="120" TextChanged="valueD_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,130,0,0" Name="valueE" VerticalAlignment="Top" Width="120" TextChanged="valueE_TextChanged" />
<TextBlock Height="23" HorizontalAlignment="Left" Margin="11,131,0,0" Name="textBlock5" Text="E" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="235,130,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" SelectionChanged="dataGrid1_SelectionChanged" Loaded="dataGrid1_Loaded" />
</Grid>
我添加了一个断点,但是代码没有被执行.我正在使用Loaded事件,是否应该使用其他事件?
I added a breakpoint and the code isn't being executed. I am using the Loaded event, should I be using a different event?
推荐答案
您的代码在 Loaded
事件中工作正常,但是在您设置 AutoGenerateColumns ="False"
和不要定义列.您需要手动定义列
Your code works fine with Loaded
event but at the moment you set AutoGenerateColumns="False"
and don't define columns. You need to either define columns manually
<DataGrid AutoGenerateColumns="False" ... Loaded="dataGrid1_Loaded">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTextColumn Binding="{Binding Value}" Header="Value"/>
</DataGrid.Columns>
</DataGrid>
或让它自动生成列
<DataGrid AutoGenerateColumns="True" ... Loaded="dataGrid1_Loaded"/>
这篇关于DataGrid不使用C#在WPF中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!