繁体   English   中英

在后面的代码中设置WPF对象的动态大小

[英]Set Dynamic Size of WPF-Object in Code Behind

我目前正在尝试设计如下所示的条形图:每行的行数和条形数可能会根据我的图表所依赖的dataTable而有所不同。 (间隙不是故意的,但不是我的问题自动取款机)

条形图

我正在尝试通过网格实现我的BarChart。 我的数据表为我提供了一个键,一个长度,一个名称和一个父项(这是右侧栏的键,因为每一行都将右侧栏拆分成较小的栏)。

网格大小当前基于我的maingrid,在XAML-ViewModel中将其设置为900 * 400。 我的小节当前设置为它们的Length / MaxLength * GridLength(而第一个小节始终为MaxLength)并具有边距,因为我希望它们从右向左移动。 我的问题是,我想使该网格的尺寸动态变化,但是如果我没有为MainGrid设置固定尺寸,则Grid的尺寸将为NaN或0。

我删除了一些令人困惑的部分,但是基本上这是我后面的代码:

public MainWindow()
{
    InitializeComponent();
    myTable = new DataTable();
}

InitChart(DataTable dt) //Is called from another class when the DataTable changes
{
    Maingrid.Children.Clear(); //Dispose old BarChart
    myTable = dt;
    Grid grid1 = new Grid();
    grid1.Width = MainGrid.Width;
    grid1.Height = MainGrid.Height;
    // Create Rows 
    int Pa = -1;
        foreach (DataRow rw in myTable.Rows)
        {    

            if (Pa != (int)rw["Parent"])
            {
                RowDefinition rdef = new RowDefinition();
                rdef.Height = new GridLength(20);
                grid.RowDefinitions.Add(rdef);
            }
            Pa = (int)rw["Parent"];
        }

    foreach (DataRow row in myTable.Rows)
    {
     int P = (int)row["Parent"];
       foreach (DataRow rw in myTable.Rows)
        {    
            if ((int)rw["Parent"] == P) //Each row splits the right bar of the previous row, all bars in one row have the previous split bar as their Parent. I draw all Bars with the same Parent in one Row. 
            {
                if ((int)rw["Val"] != 0) //Some Bars are disabled or 0. I don't draw them
                {
                    Rectangle rect = new Rectangle();
                    double L = rw["Val"]; //The Value of one Bar
                    rect.Width = L / nMaxValue * grid.Width; //Calculate width of my Rectange based on the maximal value one bar can have
                    rect.Height = 20; //Just a height
                    rect.Margin = new Thickness( grid.Width - nRowValue/nMaxValue * grid.Width, 0, 0, 0); //Margin to display the bars next to each other
                    rect.Fill = Col;
                    rect.HorizontalAlignment = HorizontalAlignment.Left; //Although i want to have my BarChart go from right to left, I align all bars from on the left and then move them with my margin
                    Grid.SetRow(rect, y);
                    nRowValue = nRowValue - (int)rw["Val"];
                    grid1.Children.Add(rect);
                }
            }
        }
    }

    MainGrid.Children.Add(grid1);
}

XAML:

<UserControl x:Class="WpfApp.MainWindow"
    <Grid x:Name="MainGrid"  Background="#f0f0f0" Height="400" Width="900" />
</UserControl>

我也已经尝试过使用ItemsControl,但似乎无法正常工作。

要使调整大小动态化,可以使用“网格”和“网格列”。 网格是一种出色的布局控件,因为它可以使用所有可用空间。 只需确保不要在任何时候设置其宽度即可。 这样,外部控件(如Page或Window)可以控制它使用多少空间。

过程:

  1. 通过在数据表中找到最大的Length来确定maxLength值。
  2. 将那么多网格列添加到网格中。 确保不设置列宽。 以下代码与添加* -column相同,这是我们需要的:

     for (int i = 0; i < maxLength; i++) { MyGrid.ColumnDefinitions.Add(new ColumnDefinition()); } 
  3. 对于每个条,创建矩形,但不要设置其宽度或边距。 而是设置Column和ColumnSpan。 例如:

     var rectangle = new Rectangle { Fill = new SolidColorBrush(Colors.LightGray), Opacity = 0.5, Margin = new Thickness(0, -10, 0, 0), Height = 66 }; MyGrid.SetColumn(rectangle, (int)maxLength - data.Length); MyGrid.SetColumnSpan(rectangle, data.Length); 

希望这可以帮助。 如果您遇到任何问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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