WPF DataGrid 添加行但不添加数据(空白行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28711940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid adds rows but not the data (blank rows)
提问by Kyle
Alright so this is going to be hard to explain.
好吧,这将很难解释。
Scenario:
设想:
I have a DataGrid defined as follows:
我有一个 DataGrid 定义如下:
<DataGrid Height="100" Name="test" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn Header="URL"></DataGridTextColumn>
<DataGridTextColumn Header="PORT"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>


It has two headers and I need to add data, I've done a lot of research that suggests using an ObservableCollection since that will fire the DataChanged event.
它有两个标题,我需要添加数据,我做了很多研究,建议使用 ObservableCollection,因为这会触发 DataChanged 事件。
I can formulate this data with long boring code that probably isn't relevant (you'll see why below).
我可以用可能不相关的冗长乏味的代码来制定这些数据(你会在下面看到原因)。
Problem:
问题:
The 200 rows are added but the text itself isn't in the table
添加了 200 行,但文本本身不在表格中


Troubleshooting:
故障排除:
I turned on debugging and with screenshots help, you can see there is actual data but it won't insert it into the rows, but it adds the rows.
我打开调试并通过屏幕截图帮助,您可以看到有实际数据,但它不会将其插入到行中,而是添加了行。


Relevant Code:
相关代码:
ObservableCollection<Proxy> collection = new ObservableCollection<Proxy>();
collection = GetData(ips,ports);
test.CanUserAddRows = true;
test.ItemsSource = null;
test.ItemsSource = collection;
MessageBox.Show("Done");
I'm banging my head against the wall trying to figure this out.
我正用头撞墙试图弄清楚这一点。
NOTE:I added the .ItemSource = null; and then set it equal to the collection as that enabled the rows to show up. I've tried using the suggested: test.DataContext = collection; But, no rows get added at all, and yes just as this test the data is visible in debug mode as being part of the context/itemsource.
注意:我添加了 .ItemSource = null; 然后将它设置为等于集合,因为这使行能够显示。我试过使用建议的: test.DataContext = collection; 但是,根本没有添加任何行,是的,就像这个测试一样,数据在调试模式下是可见的,作为上下文/项目源的一部分。
UPDATE:
更新:
I've tried implementing the following XAML with the same results
我已经尝试以相同的结果实现以下 XAML
<DataGrid Height="100" Name="test" IsReadOnly="False" ItemsSource="{Binding collection}">
<DataGrid.Columns>
<DataGridTextColumn Header="URL" Binding="{Binding ip}"></DataGridTextColumn>
<DataGridTextColumn Header="PORT" Binding="{Binding port}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Proxy Class:
代理类:
public class Proxy
{
public string ip;
public string port;
}
UPDATE 2:When adding get and set methods the following was my result:
更新 2:添加 get 和 set 方法时,我的结果如下:


回答by
You are missing to define the Properties for the bindings: at least, change the Proxy class like this:
您缺少为绑定定义属性:至少,像这样更改 Proxy 类:
public class Proxy
{
public string ip {get; set;}
public string port {get; set;}
}
回答by Philip Stuyck
You have no binding for your columns. This is an example on how to do it :
您的列没有绑定。这是有关如何执行此操作的示例:
<DataGrid x:Name="RelatieSearchGrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Relaties.View}" IsReadOnly="True"
SelectionMode="Single">
<DataGrid.Columns>
<DataGridTextColumn Header="Naam" Binding="{Binding Naam}"/>
<DataGridTextColumn Header="Straat" Binding="{Binding Straat}"/>
<DataGridTextColumn Header="Postcode" Binding="{Binding Postcode}"/>
<DataGridTextColumn Header="Gemeente" Binding="{Binding Gemeente}"/>
<DataGridTextColumn Header="BTW" Binding="{Binding BTW}"/>
</DataGrid.Columns>
</DataGrid>
In your case since the collection contains proxy, you need to bind to the proxy members. You didn't show the proxy code so I can only guess what those members are. If you want to go via the datacontext, then you need to work with a viewmodel. In that case I suggest you read something about MVVM first.
在您的情况下,由于集合包含代理,您需要绑定到代理成员。你没有显示代理代码,所以我只能猜测那些成员是什么。如果您想通过数据上下文,那么您需要使用视图模型。在这种情况下,我建议您先阅读一些有关 MVVM 的内容。
回答by user3596113
You should first set the ItemsSource in xaml:
您应该首先在 xaml 中设置 ItemsSource:
<DataGrid ItemsSource="{Binding test}" ... >
Then you should add your TemplateColumns like this:
然后你应该像这样添加你的 TemplateColumns:
<DataGrid ItemsSource="{Binding test}" ...>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Url}"
Header="URL" />
<DataGridTextColumn Binding="{Binding Port}"
Header="Port" />
</DataGrid.Columns>
</DataGrid>
Note that this will need your Objects stored in the ObservableCollection testto have an Url and a Port Porperty.
请注意,这将需要存储在 ObservableCollection 中的对象test具有 URL 和端口属性。
Change your Model class to implement INPC and adjust your properties like this:
更改您的模型类以实现 INPC 并像这样调整您的属性:
public class Proxy : INPC
{
public String Url
{
get
{
return url;
}
set
{
if (url== value) {
return;
}
url= value;
RaisePropertyChanged("Url");
}
}
private string url;
public String Port
{
get
{
return port;
}
set
{
if (port== value) {
return;
}
port= value;
RaisePropertyChanged("Port");
}
}
private string port;
}

