locked
TextBlock dentro de rectangle em tempo de execução RRS feed

  • Pergunta

  • Saudações


    TextBlock dentro de rectangle em tempo de execução:

    preciso fazer isso, até consegui em partes. O que acontece é que o texto nao fica centralizado direito. Principalmente quando rotaciono o rectangle:

      #region Desenha a "chapa de vidro" com dimensões fixas 611x356
    
                        UIElement retangulo = new Rectangle() { Width = (double)largura, Height = (double)altura, Fill = new SolidColorBrush(Colors.LightGray) };
    
                        System.Windows.Controls.Canvas.SetLeft(retangulo, 0);
                        System.Windows.Controls.Canvas.SetTop(retangulo, 0);
                        canvas.Children.Add(retangulo);
    
                        #endregion
    
                        int index = 1;
    
                        foreach (var pecaMapeada in PecasMapeadas)
                        {
                            Rectangle ret = new Rectangle()
                            {
                                Width = (double)pecaMapeada.Width,
                                Height = (double)pecaMapeada.Height,
                                Fill = new SolidColorBrush(Colors.White),
                                StrokeThickness = 1,
                                Stroke = new System.Windows.Media.SolidColorBrush(Colors.Gray),
                                Name = "ret" + index
                            };
                            System.Windows.Controls.Canvas.SetLeft(ret, (double)pecaMapeada.X);
                            System.Windows.Controls.Canvas.SetTop(ret, (double)pecaMapeada.Y);
    
                            ret.MouseLeftButtonDown += new MouseButtonEventHandler(Handle_MouseDown);
                            ret.MouseMove += new MouseEventHandler(Handle_MouseMove);
                            ret.MouseLeftButtonUp += new MouseButtonEventHandler(Handle_MouseUp);
    
                            dimensoes.Add(new Dimension(Canvas.GetLeft(ret), Canvas.GetTop(ret), ret.Name, ret.GetType(), ret));
    
                            canvas.Children.Add(ret);
    
                            var nomePecaTextBlock = new TextBlock
                            {
                                FontSize = 11,
                                Text = pecaMapeada.WidthToString + " X " + pecaMapeada.HeightToString,
                                Foreground = new System.Windows.Media.SolidColorBrush(Colors.Gray),
                                Margin = new Thickness(5),
                                TextAlignment = TextAlignment.Center,
                                VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
                                HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
                                FontWeight = FontWeights.Bold,
                                TextWrapping = TextWrapping.Wrap,
                                Name = ret.Name + "NomeText"
                            };
    
                            if (((Rectangle)ret).Width < ((Rectangle)ret).Height)
                            {
                                nomePecaTextBlock.RenderTransform = new RotateTransform() { Angle = -90 };
                            }
    
                            System.Windows.Controls.Canvas.SetLeft(nomePecaTextBlock, System.Windows.Controls.Canvas.GetLeft(((Rectangle)ret)) + (((Rectangle)ret).Width - nomePecaTextBlock.ActualWidth) / 2);
                            System.Windows.Controls.Canvas.SetTop(nomePecaTextBlock, System.Windows.Controls.Canvas.GetTop(((Rectangle)ret)) + (((Rectangle)ret).Height - nomePecaTextBlock.ActualHeight) / 2);
    
                            canvas.Children.Add(nomePecaTextBlock);
    
                            index++;
    
                            var pmMain = new PopupMenu { OpenOnAccessKeyPressed = true };
                            pmMain.AddItem("Inverter", delegate
                            {
                                double w = ret.Height;
                                double h = ret.Width;
    
                                ret.Width = w;
                                ret.Height = h;
    
                                canvas.Children.Remove(ret);
                                canvas.Children.Remove(nomePecaTextBlock);
    
                                nomePecaTextBlock = new TextBlock
                                {
                                    FontSize = 11,
                                    Text = pecaMapeada.WidthToString + " X " + pecaMapeada.HeightToString,
                                    Foreground = new System.Windows.Media.SolidColorBrush(Colors.Gray),
                                    Margin = new Thickness(5),
                                    TextAlignment = TextAlignment.Center,
                                    HorizontalAlignment = HorizontalAlignment.Center,
                                    VerticalAlignment = VerticalAlignment.Center,
                                    FontWeight = FontWeights.Bold,
                                    TextWrapping = TextWrapping.Wrap,
                                    Name = ret.Name + "NomeText"
                                };
    
                                if (((Rectangle)ret).Width < ((Rectangle)ret).Height)
                                {
                                    nomePecaTextBlock.RenderTransform = new RotateTransform() { Angle = -90 };
                                }
    
                                System.Windows.Controls.Canvas.SetLeft(nomePecaTextBlock, (System.Windows.Controls.Canvas.GetLeft(((Rectangle)ret)) + (((Rectangle)ret).ActualWidth - nomePecaTextBlock.ActualWidth) / 2));
                                System.Windows.Controls.Canvas.SetTop(nomePecaTextBlock, (System.Windows.Controls.Canvas.GetTop(((Rectangle)ret)) + (((Rectangle)ret).ActualHeight - nomePecaTextBlock.ActualHeight) / 2));
    
                                Debug.WriteLine(
                                    "ret atual W: " + ret.Width + Environment.NewLine +
                                    "ret atual H: " + ret.Height + Environment.NewLine +
                                    "ret atual X: " + System.Windows.Controls.Canvas.GetLeft(((Rectangle)ret)) + Environment.NewLine +
                                    "ret atual Y: " + System.Windows.Controls.Canvas.GetTop(((Rectangle)ret)) + Environment.NewLine +
                                    "nomePecaTextBlock atual W: " + nomePecaTextBlock.ActualWidth + Environment.NewLine +
                                    "nomePecaTextBlock atual H: " + nomePecaTextBlock.ActualHeight + Environment.NewLine +
                                    "nomePecaTextBlock atual X: " + System.Windows.Controls.Canvas.GetLeft(nomePecaTextBlock) + Environment.NewLine +
                                    "nomePecaTextBlock atual Y: " + System.Windows.Controls.Canvas.GetTop(nomePecaTextBlock));
    
                                canvas.Children.Add(ret);
                                canvas.Children.Add(nomePecaTextBlock);
                            });
    
                            pmMain.AddSeparator();
    
                            pmMain.AddItem("Remover", delegate
                            {
                                canvas.Children.Remove(ret);
                                canvas.Children.Remove(nomePecaTextBlock);
                            });
    
                            pmMain.AddTrigger(TriggerTypes.RightClick, ret);
    
                            //pmMain.Showing += (senderPm, ePm) =>
                            //{
                            //    pmMain.PopupMenuItem(0).Header = "Remover";
                            //    pmMain.PopupMenuItem(0).Click += delegate
                            //    {
                            //        canvas.Children.Remove(ret);
                            //        canvas.Children.Remove(nomePecaTextBlock);
                            //    };
                            //};
                        }
                    };
                };
            }


    Desenvolva!!!

    quarta-feira, 19 de setembro de 2012 18:10