【问题标题】:WPF Toolkit - having problem Binding in the DataGrid controlWPF 工具包 - 在 DataGrid 控件中绑定问题
【发布时间】:2011-07-07 17:33:29
【问题描述】:

首先我必须说,这可能看起来像很多代码,但很容易阅读。 我正在尝试绑定一些东西,结果我得到了这个:

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;
    }
}

如何修复这些重复项?

【问题讨论】:

    标签: c# datagrid binding wpftoolkit


    【解决方案1】:

    尝试添加AutoGenerateColumn="False"

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

    【讨论】:

    • 哦,我不敢相信。在过去的 2 个小时里,我一直在做这件事。该死的:(,无论如何,谢谢。
    【解决方案2】:

    尝试设置 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;
        }
    }
    

    【讨论】:

    • 我也会去掉构造函数,但为了保持连续性,我把它留了下来。
    • 你将如何摆脱构造函数?
    • 一个对象初始化器,它是一个 C# 3.0 特性(如自动道具)。像这样:var de = new DebugError { Number=0, Description="TACO", Line="Three?", Column="Food Awesomeness"};
    • 嘿等等,把它作为一个 SO 问题发布!
    • 感谢 Ritch 的帮助,虽然我认为这不值得提问,但我会使用你所说的一切 :)
    猜你喜欢
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2011-07-10
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多