我试图从datagrid(wpf)中的web服务列出数据,但我不知道我做错了什么,或者给我错误或者没有列出任何内容。 这就是我现在的样子。
WINDOWS.XAML
<Window x:Class="IssueAddinOutlook.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Issue List" Height="424" Width="696">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="102*" />
<ColumnDefinition Width="590*" />
</Grid.ColumnDefinitions>
<Label Height="41" Margin="172,0,265,0" Name="label1" VerticalAlignment="Top" FontSize="22" Grid.Column="1">Issue List</Label>
<dg:DataGrid x:Name="dataGrid" AutoGenerateColumns="True"
AlternationCount="2"
HeadersVisibility="All"
HorizontalGridLinesBrush="#DDDDDD"
VerticalGridLinesBrush="#DDDDDD" Grid.ColumnSpan="2" Margin="0,0,28,26">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="ID Issue" Binding="{Binding Path=Id}" />
<dg:DataGridTextColumn Header="Order Id" Binding="{Binding Path=OrderId}" />
<dg:DataGridTextColumn Header="Is Done" Binding="{Binding Path=IsDone}" />
<dg:DataGridTextColumn Header="Final Comment" Binding="{Binding Path=FinalComment}" />
<dg:DataGridTextColumn Header="Actual Hours" Binding="{Binding Path=ActualHours}" />
<dg:DataGridTextColumn Header="Group Id" Binding="{Binding Path=GroupId}" />
</dg:DataGrid.Columns>
</dg:DataGrid>
</Grid>
<Window.Resources>
<Style x:Key="columnHeaderStyle" TargetType="{x:Type dg:DataGridColumnHeader}">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<LinearGradientBrush.GradientStops>
<GradientStop Color="Navy" Offset="0" />
<GradientStop Color="LightBlue" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="White" />
</Style>
<Style x:Key="rowStyle" TargetType="dg:DataGridRow">
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="10" />
<Style.Triggers>
<Trigger Property="AlternationIndex" Value="0">
<Setter Property="Background" Value="White" />
</Trigger>
<Trigger Property="AlternationIndex" Value="1">
<Setter Property="Background" Value="#DDDDDD" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#BBBBBB" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
和WINDOWS.XAML.CS
Issuereference.Tasks issueRef = new Issuereference.Tasks();
Issuereference.TASK[] tasksList = issueRef.GetTasks(39);
dataGrid.ItemsSource = tasksList.ToList();
使用此代码提供错误在使用ItemsSource之前,Items集合必须为空。 任何人都可以帮助我吗?
答案 0 :(得分:1)
您的代码似乎运行正常。无论如何,分配ItemsSource,网格中是否有任何内容?或者是否有应用程序的任何部分手动将项目添加到DataGrid?该错误消息通常意味着应用程序的一部分已更新ItemsControl的集合,同时它具有已分配的ItemsSource。希望这是有道理的。