c# - 修改wpf中任何矩形的高度后如何重新排列 Canvas 上的矩形?

标签 c# wpf canvas drawrectangle

我正在从用户直接在网格行中输入的网格单元格值添加矩形。当我修改特定列的值时,例如 ThicknessHeight 然后它会增加所选行矩形的 Height 但它不会重新排列其下方的所有矩形在选定的行矩形之后。

在 xaml.cs 中

public class MyLayer : INotifyPropertyChanged 
{

    public string Thickness { get; set; }
    public string OffsetRight { get; set; }
    public string OffsetLeft { get; set; }
    public string Material { get; set; }
    public string MaterialPopup { get; set; }
    public Rectangle rectangle { get; set; }

    public GlassRectangle GlassRectangle { get; set; }
    public MaterialLayer()
    {
        GlassRectangle = new GlassRectangle();

    }
    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
        add { }
        remove {  }
    }
}

public class GlassRectangle
{

    public Rectangle Rectangle { get; set; }
    public double Top = 0;
    public GlassRectangle()
    {
        Rectangle = new Rectangle();

    }
}

private void gridInner_CellValueChanged(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e)
        {
            string cellValue = string.Empty;
            MyLayer currentLayer = ((MyLayer)(e.Row));

            if (e.Column.HeaderCaption.ToString() == "Thickness")
            {

                cellValue =(e.Value.ToString());
                //there is alredy a rectangle - means this is edit mode
                if (currentLayer.rectangle != null)
                {
                    currentLayer.rectangle.Height = Convert.ToDouble(cellValue);
                    currentLayer.rectangle.Stroke = new SolidColorBrush(Color.FromRgb(0, 255, 0));

                }
                //else this is insert mode
                else
                {

                    currentLayer.rectangle = CreateRectangle(cellValue);
                }

            }

        }

 protected Rectangle  CreateRectangle(string cellval)
        {
            Rectangle newrect = new Rectangle();
            newrect.Stroke = Brushes.Red;
            newrect.StrokeThickness = 1;
            if (cellval.ToString().Contains("."))
            {
                newrect.Height = Convert.ToDouble(cellval) * 100;
            }
            else
            {
                newrect.Height = Convert.ToDouble(cellval);
            }
            newrect.Width = width;
            Canvas.SetLeft(newrect, 100);
            double canvasTop = 0.0;
            if (canvasboard.Children.Count > 0)
            {
                var lastChildIndex = canvasboard.Children.Count - 1;
                var lastChild = canvasboard.Children[lastChildIndex] as FrameworkElement;
                if (lastChild != null)
                    //lastChild.Height-1: so that it come extactly on existing if set to +1 it comes below first rectangle
                    canvasTop = Canvas.GetTop(lastChild) + lastChild.Height - 1;
            }

            Canvas.SetTop(newrect, canvasTop);
            val = val + 1;
            newrect.Tag = val;
            canvasboard.Children.Add(newrect);
            //rectangle = rect;

            foreach (UIElement ui in canvasboard.Children)
            {
                if (ui.GetType() == typeof(Rectangle))
                {
                    itemstoremove.Add(ui);
                }
            }

            return newrect;
        }

新事件方法:

private void gridMaterialInner_CellValueChanged(object sender, DevExpress.Xpf.Grid.CellValueChangedEventArgs e)
        {
            string cellValue = string.Empty;
            string cellOldValue = string.Empty;
            MyLayer currentLayer = ((MyLayer)(e.Row));
            if (e.Column.HeaderCaption.ToString() == "Thickness")
            {
                //current cell value
                cellValue =(e.Value.ToString());// GetRowCellValue(e.RowHandle, gridMaterialInner.Columns["LastName"]).ToString();
                //there is alredy a rectangle - means this is edit mode
                double currentheight = 0.0;
                double oldht = 0.0;
                // old cell value
                if (e.OldValue != null)
                {
                    cellOldValue = (e.OldValue.ToString());
                }
                if (currentLayer.rectangle != null)
                {
                    if (cellValue.ToString().Contains("."))
                    {
                        currentheight = Convert.ToDouble(cellValue) * 100;
                    }
                    else
                    {
                        currentheight = Convert.ToDouble(cellValue) * 100;
                    }
                    if (cellOldValue.ToString().Contains("."))
                    {
                        oldht = Convert.ToDouble(cellOldValue) * 100;
                    }
                    else if(cellOldValue!=string.Empty)
                    {
                        oldht = Convert.ToDouble(cellOldValue) * 100;
                    }

                    currentLayer.rectangle.Height = currentheight;
                    currentLayer.rectangle.Stroke = new SolidColorBrush(Color.FromRgb(0, 255, 0));

                    //Refresh();
                    //Get the index of selected row
                    int layerIndex = materialBindlist.IndexOf(currentLayer);
                    for(int i = layerIndex; i < materialBindlist.Count-1; i++)
                    {
                        //set the top position of all other rectangles that are below selected rectangle/row
                        //(Current-Old)+Top
                        Canvas.SetTop(materialBindlist[i + 1].rectangle, (currentheight - oldht) + materialBindlist[i + 1].GlassRectangle.Top);
                        //Canvas.SetTop(materialBindlist[i].rectangle, (currentheight - oldht) + materialBindlist[i + 1].GlassRectangle.Top);

                    }
                }
                //else this is insert mode
                else
                {
                    //MaterialLayer object
                    currentLayer.rectangle = CreateRectangle(cellValue);
                    //store Top & Rectangle object in GlassRectangle class which is referenced in MaterialLayer class
                    currentLayer.GlassRectangle.Rectangle = currentLayer.rectangle;
                    currentLayer.GlassRectangle.Top = canvasTop;
                }

            }

        }

这将创建一个又一个的矩形,就像 Canvas 上的堆叠项目一样。但是,当我修改 Thickness 列的值(即 矩形Height)时,它会反射(reflect)在 Canvas 上,但下面的其他矩形必须出现在更改的高度之后当前矩形。

注意:我无法在我的应用程序中使用 WrapPanel。只是使用 Canvas 修改现有代码。

感谢帮助!

修改 CellChange 事件中的 For 循环:

int layerIndex = materialBindlist.IndexOf(currentLayer);
                    for(int i = layerIndex; i < materialBindlist.Count-1; i++)
                    {
                        //set the top position of all other rectangles that are below selected rectangle/row
                        //(Current-Old)+Top
                        double top=Convert.ToDouble((currentHeight - oldHeight) + materialBindlist[i + 1].GlassRectangle.Top);
                        Canvas.SetTop(materialBindlist[i + 1].rectangle,top);

                        materialBindlist[i + 1].GlassRectangle.Top = top;



                    }

最佳答案

即使使用 Canvas 也可以完成您正在寻找的内容但是你真的应该考虑使用类似 ItemsControl 的东西为此。

强制使用Canvas时的解决方案:

private void Refresh() {
  for (int i = 1; i < canvasboard.Children.Count; ++i) {
    var currentElement = canvasboard.Children[i] as FrameworkElement;
    var previousElement = canvasboard.Children[i - 1] as FrameworkElement;
    if (currentElement == null || previousElement == null)
      return;
    var requiredTop = Canvas.GetTop(previousElement) + previousElement.Height - 1;
    if (Math.Abs(Canvas.GetTop(currentElement) - requiredTop) > 0.0)
      Canvas.SetTop(currentElement, requiredTop);
  }
}

现在,在更改 Canvas 中现有元素的大小“之后”调用此函数并且它将相应地重新定位元素以适应新的维度。在您的代码中,它将从 gridInner_CellValueChanged(...) 调用。在“编辑”模式下设置新高度后的函数。

你应该尝试做什么:

如果您能够说服任何您需要的人并使用类似 ItemsControl 的东西,这样就简单多了。

举个粗略的例子:

xaml 可能是:

<ItemsControl ItemsSource="{Binding Items}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <VirtualizingStackPanel Orientation="Vertical" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

Items声明为public ObservableCollection<Rectangle> Items { get; set; }在代码中。

现在你的Add()函数可能只是:

private void Add() {
  var rect = new Rectangle {
    Stroke = Brushes.Red,
    StrokeThickness = 1,
    Height = Convert.ToDouble(txtheight.Text),
    Width = 100
  };
  Items.Add(rect);
}

至于编辑现有控件时的更新,在这种情况下将是自动的。没有硬编码的定位,因为布局容器会为您处理所有困惑的事情。

您当然可以切换 Items集合类型为您自己的自定义控件类型 MyLayer随着 INPC 的实现,变化仍将继续自动进行。您必须定义 DataTemplate现在要渲染您的 Item,但这就像 xaml 中的 3 行工作。

您也可以只使用Items当需要调整现有控件时,可以直接使用属性,而不是引用 ItemsControl在代码隐藏中。绑定(bind)应该自动处理 View 的更新。

关于c# - 修改wpf中任何矩形的高度后如何重新排列 Canvas 上的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17654270/

相关文章:

C# 比较两个相同对象类型的列表

wpf - winforms,WPF和metro之间的区别?

html - 将 CSS Resize 属性添加到 Canvas 元素

animation - 如何创建彩色条纹背景,动画缓慢旋转 360 度,不使用光栅图像或 js?

c# - 如何使用 Azure Active Directory Graph API 获取属于 AppRole 的所有用户

C# - 如何删除具有命名空间的 XMLDocument 中的声明和元素?

c# - WPF 的 Windows 资源管理器控件?

c# - 使用 IDataErrorInfo 在 MVVM 中执行验证时,我应该在哪里执行检查以查看数据库中是否已存在值?

javascript - 使用类方法在 HTML5 Canvas 上绘图 : scope problems (JS, ES6)

C# WinForms DataGridView 背景颜色渲染太慢