WPF工具包 - 在DataGrid控件中有问题绑定

时间:2011-03-03 01:27:48

标签: c# datagrid binding wpftoolkit

嘿伙计们, 首先,我必须说,这可能看起来很多代码,但它很容易阅读。 我试图绑定一些东西,我得到了这个结果:

http://img694.imageshack.us/f/28475988.jpg/

正如您所看到的那样,数字,描述,行和列似乎是重复的。

我的主表单设计师

<Window x:Class="Visual_Command_Line.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Visual_Command_Line"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Visual Command Line" MinHeight="750" MinWidth="900" Loaded="Window_Loaded" Icon="/Visual_Command_Line;component/Resources/icon16x16.ico" WindowStartupLocation="CenterScreen" WindowState="Maximized" Closing="Window_Closing">
<Window.Resources>
    <local:ErrorListCollection x:Key="ErrorList" />
</Window.Resources>
                        <dg:DataGrid Name="DataGrid_ErrorList" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" CanUserSortColumns="False" ItemsSource="{Binding Source={StaticResource ErrorList}}">
                            <dg:DataGrid.Columns>
                                <dg:DataGridTextColumn Binding="{Binding Path=GetNumber}" Header="" />
                                <dg:DataGridTextColumn Binding="{Binding Path=GetDescription}" Header="Description" Width="10*" />
                                <dg:DataGridTextColumn Binding="{Binding Path=GetLine}" Header="Line" Width="*" />
                                <dg:DataGridTextColumn Binding="{Binding Path=GetColumn}" Header="Column" Width="*" />
                            </dg:DataGrid.Columns>
                        </dg:DataGrid>
</Grid>

当主表单加载时我执行:

((ErrorListCollection)this.FindResource("ErrorList")).RenewErrorList(((TabDocument)dockManager.ActiveDocument).currentAnalizedLine);

这是ErrorListCollection类:

class ErrorListCollection : ObservableCollection<DebugError>
{
    public ErrorListCollection()
    {
    }

    public void RenewErrorList(AnalizedLine al) //also all lines
    {
        this.Clear();

        int currentAnalizedLine_lineNumber = al.lineNumber;

        int errNumber = 1;
        foreach (Boundaries b in al.GetBoundaries)
        {
            if (b.dataType == Boundaries.DataType.Unknown)
            {
                this.Add(new DebugError(errNumber, "d", "l", "c"));
                errNumber++;
            }
        }
    }
}

DebugError类:

class DebugError
{
    private int Number;
    private string Description;
    private string Line;
    private string Column;

    public int GetNumber
    {
        get
        {
            return this.Number;
        }
    }

    public string GetDescription
    {
        get
        {
            return this.Description;
        }
    }

    public string GetLine
    {
        get
        {
            return this.Line;
        }
    }

    public string GetColumn
    {
        get
        {
            return this.Column;
        }
    }

    public DebugError(int Number, string Description, string Line, string Column)
    {
        this.Number = Number;
        this.Description = Description;
        this.Line = Line;
        this.Column = Column;
    }
}

如何修复此重复项?

感谢先进,

锭。

2 个答案:

答案 0 :(得分:5)

尝试添加AutoGenerateColumn="False"

<dg:DataGrid Name="DataGrid_ErrorList" AutoGenerateColumns="False"

答案 1 :(得分:3)

尝试设置AutoGenerateColumns =“False”

您也可以稍微简化DebugError类:

class DebugError
{
    public int Number { get; private set; }
    public string Description { get; private set; }
    public string Line { get; private set; }
    public string Column { get; private set; }

    public DebugError(int number, string description, string line, string column)
    {
        Number = number;
        Description = description;
        Line = line;
        Column = column;
    }
}