简体   繁体   中英

List data from a webservice in datagrid WPF

i am trying to list data from a webservice in a datagrid (wpf), but i dont know what i doing wrong, or gives me errors or dont list anything. This is how i have right now.

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>

And the WINDOWS.XAML.CS

 Issuereference.Tasks issueRef = new Issuereference.Tasks();
 Issuereference.TASK[] tasksList = issueRef.GetTasks(39);
 dataGrid.ItemsSource = tasksList.ToList();

with this code gives the error Items collection must be empty before using ItemsSource. Can anyone help me?

Your code seems to work fine. Anyway, assigning the ItemsSource, is there anything that appears already inside the grid? Or is there any part of the application that manually adds items to the DataGrid? That error message usually means that a part of the application has updated the ItemsControl's collection while it has an assigned ItemsSource. Hope this makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM