2

编辑解决:

由于对答案和评论的洞察力,我能够找到适合我需求的解决方案。由于数据网格是动态创建的并在循环中重复使用,以便可以将其插入树视图中,因此我需要做的大部分事情都无法在 XML 中完成(据我所知,花了半天时间尝试找到一种方法来完成这项工作,但它并没有发生。)由于 HighCore 轻视我的代码(这是必要的并且基于事实,但让我舔了舔伤口,因为我为此感到自豪)我继续前进一种数据绑定方法。如果您没有耐心阅读本段的其余部分,代码示例如下。它使我的代码更加简洁,并允许我将 Binded DataGridTextColumns 添加到可以使用代码设置器格式化的 DataGrid. 我能够通过引用 Sheridan 的答案在代码中创建 setter,这给了我所需的一切。代码如下。

while (bc.next != null)
{
    bc = bc.next;
    System.Windows.Controls.DataGrid dgbc = new System.Windows.Controls.DataGrid()
    dgbc.AutoGenerateColumns = false;

    Style style = new Style(typeof(System.Windows.Controls.DataGridCell));
    Setter setter = new     Setter(System.Windows.Controls.DataGridCell.HorizontalAlignmentProperty, System.Windows.HorizontalAlignment.Right);
    style.Setters.Add(setter);

    DataGridTextColumn dgtc = new DataGridTextColumn();
    dgtc.Binding = new System.Windows.Data.Binding("pBudgetCode");
    dgtc.Header = "Budget Code";
    dgbc.Columns.Add(dgtc);

    DataGridTextColumn dgtc2 = new DataGridTextColumn();
    dgtc2.Binding = new System.Windows.Data.Binding("pOriginalEstimate");
    dgtc2.CellStyle = style;
    dgtc2.Header = "Original Estimate";
    dgbc.Columns.Add(dgtc2);

    //More Columns added, dgtc above intentionally did not use the style.

    LinkedList<BudgetCodeCTC> tempCode = new LinkedList<BudgetCodeCTC>();
    tempCode.AddLast(bc.getCTCProps());
    dgbc.ItemsSource = tempCode;

    //BudgetCodeCTC is the class that contains the properties to be bound.
    //The paramater in creation of the Binding object is the name of the Property in BudgetCodeCTC that the column is bound to.

    foreach(BudgetCategoryCTC catCTC in tempCode.ElementAt(0).pAccumulateAs)
    {
         //Essentially same code as above except applied at the next level down in the TreeView and then added to the Parent TreeViewItem

          //Another foreach loop for the next level down using essentially the same code
    }

我知道这只是代码的快照,但希望它为寻找类似解决方案的人提供足够的信息。

这看起来像的例子:

在此处输入图像描述

  • 我省略了一些东西并解释了用//指定的注释中省略的简单版本
  • 虽然循环测试是在使用 .next 向对象添加链表功能而没有所有开销的对象上进行的(我知道我是少数这样做而不是使用 LinkedList<>,我怀疑我会说服你用我的方式,我知道你不会说服我)。
  • 还引用了 winforms,因为它们在省略的区域中使用,因为最终用户的偏好(打了好仗却输了)。我删除了一些 System.Windows.Controls 所以你不必滚动代码框太远,我向你保证代码编译并运行。

            while (bc.next != null)
            {
                bc = bc.next;
                budgetCodeCategory mat = bc.materials;
                budgetCodeCategory equ = bc.equipment;
                budgetCodeCategory sub = bc.sub;
                budgetCodeCategory oth = bc.other;
                budgetCodeCategory lab = bc.labor;
                budgetCodeCategory ovh = bc.overhead;
                DataTable t = new DataTable();
                t.Columns.Add("Budget Code", typeof(String));
                t.Columns.Add("Original Estimate", typeof(Decimal));
                t.Columns.Add("Approved Owner Changes", typeof(Decimal));
                t.Columns.Add("Total Estimate", typeof(Decimal));
                t.Columns.Add("Job-To-Date Costs", typeof(Decimal));
                t.Columns.Add("% Complete", typeof(Decimal));
                t.Columns.Add("Cost To Complete", typeof(Decimal));
                t.Columns.Add("Revised Cost At Completion", typeof(Decimal));
                t.Columns.Add("Total Estimate Variance", typeof(Decimal));
    
                //Row Added Here
    
                DataView dvbc = new DataView(t);
                DataGrid dgbc = new System.Windows.Controls.DataGrid();
                dgbc.ItemsSource = dvbc;
                TreeViewItem tvbc = new TreeViewItem() { Header = dgbc };
                tvbc.UpdateLayout();
                if (mat.first != null)
                {
                    DataTable ct = new DataTable();
                    ct.Columns.Add("Category", typeof(String));
                    ct.Columns.Add("Original Estimate", typeof(Decimal));
                    ct.Columns.Add("Approved Owner Changes", typeof(Decimal));
                    ct.Columns.Add("Total Estimate", typeof(Decimal));
                    ct.Columns.Add("Job-To-Date Costs", typeof(Decimal));
                    ct.Columns.Add("% Complete", typeof(Decimal));
                    ct.Columns.Add("Cost To Complete", typeof(Decimal));
                    ct.Columns.Add("Revised Cost At Completion", typeof(Decimal));
                    ct.Columns.Add("Total Estimate Variance", typeof(Decimal));
    
                    //Row Added Here
    
                    ct.AcceptChanges();
                    DataView dv = new DataView(ct);
                    DataGrid dg = new System.Windows.Controls.DataGrid();
                    dg.ItemsSource = dv;
                    TreeViewItem tvi = new TreeViewItem() { Header = dg };
                    tvi.UpdateLayout();
                    tvbc.Items.Add(tvi);
                    if (mat.first.next != null)
                    {
                        mat = mat.first;
                        Boolean myHeader = true;
                        while (mat.next != null)
                        {
                            mat = mat.next;
                            DataTable ctch = new DataTable();
                            ctch.Columns.Add("Category", typeof(String));
                            ctch.Columns.Add("Original Estimate", typeof(Decimal));
                            ctch.Columns.Add("Approved Owner Changes", typeof(Decimal));
                            ctch.Columns.Add("Total Estimate", typeof(Decimal));
                            ctch.Columns.Add("Job-To-Date Costs", typeof(Decimal));
                            ctch.Columns.Add("% Complete", typeof(Decimal));
                            ctch.Columns.Add("Cost To Complete", typeof(Decimal));
                            ctch.Columns.Add("Revised Cost At Completion", typeof(Decimal));
                            ctch.Columns.Add("Total Estimate Variance", typeof(Decimal));
    
                            //Row Added Here
    
                            ctch.AcceptChanges();
                            DataView dvc = new DataView(ctch);
                            DataGrid dgc = new System.Windows.Controls.DataGrid();
                            dgc.ItemsSource = dvc;
                            TreeViewItem tvic = new TreeViewItem() { Header = dgc };
                            tvic.UpdateLayout();
                            tvi.Items.Add(tvic);
                         }
                      }
                   }
                }
    
                //This if statement is repeated 5 more times for other Children of tvi.  That code is Identical to what is shown here(omitted Row add is different).
    
                //This is the end of the relevant code
    

对于长代码示例,我深表歉意。

所以我的问题是这一切都在窗口的构造函数中消失了。在 DataGrid 变量范围的末尾,它没有列,我需要将列格式化为右对齐。但是,当从 DataGrid 触发一个事件并且我从发送者那里获得了 Count on the Columns 时,它显示为有 9 列(正确的数字)

我尝试使用 AutoGeneratingColumn 和 AutoGeneratedColumns 事件来找出 DataGrid 何时初始化其列并且这些事件从未触发。

如果有人可以阐明一种不必触发事件来格式化列的方式,这将真正有帮助,那么我可能不得不推迟发布(仅限内部,我们不发布公共软件)由于这个问题,因为我正在清理格式,以便它看起来干净以进行测试。

谢谢你的时间。

4

1 回答 1

3

你真的不应该在代码中做这样的事情。如果您DataGrid.Columns在 XAML 中定义数据绑定到DataGrid.ItemsSource属性并仅从代码更新数据绑定集合,则会容易得多。DataGridTextColumn您可以像这样在 XAML中轻松右对齐 a :

<DataGridTextColumn Binding="{Binding SomeNumericColumn}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn> 

更新>>>

Style可以重复使用,但我们通常在 a 中定义每一列DataGrid

<Style Name="RightAlign" TargetType="{x:Type TextBlock}">
    <Setter Property="HorizontalAlignment" Value="Right" />
</Style>

...

<DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding SomeTextColumn}" />
    <DataGridTextColumn Binding="{Binding SomeNumericColumn}"
        ElementStyle="{StaticResource RightAlign}" ... />
    <DataGridTextColumn Binding="{Binding AnotherTextColumn}" />
    ...
</DataGrid.Columns>
于 2014-03-19T20:52:14.163 回答