TextBlock TextWrapping doesn't work WP 8.1 - c#

I have this XAML code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer x:Name="ScrollViewer" Grid.Row="0" Background="Red">
<StackPanel x:Name="chat" >
</StackPanel>
</ScrollViewer>
</Grid>
And I am adding TextBlocks into the StackPanel called "chat" with this code:
public void ponerMensaje(string mensaje, bool me)
{
StackPanel panelTexto = new StackPanel();
panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;
Thickness marginpanel = panelTexto.Margin;
marginpanel.Bottom = 10;
panelTexto.Margin = marginpanel;
//Create the colorBrush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Yellow;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
//Create the triangle
Polygon yellowTriangle = new Polygon();
yellowTriangle.Fill = yellowBrush;
//Create the triangle's points
System.Windows.Point Point1 = new System.Windows.Point(0, 0);
System.Windows.Point Point2 = new System.Windows.Point(10, 0);
System.Windows.Point Point3;
if (!me)
Point3 = new System.Windows.Point(10, 10);
else
Point3 = new System.Windows.Point(0, 10);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
//Add the points
yellowTriangle.Points = polygonPoints;
//Create the textblock
Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
gridParaTexto.Background = yellowBrush;
TextBlock texto = new TextBlock();
texto.TextWrapping = TextWrapping.Wrap;
texto.Text = mensaje;
texto.Foreground = blackBrush;
gridParaTexto.Children.Add(texto);
//Add the message
if (!me)
{
panelTexto.Children.Add(yellowTriangle);
panelTexto.Children.Add(gridParaTexto);
chat.Children.Add(panelTexto);
}
else
{
panelTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
panelTexto.Children.Add(gridParaTexto);
panelTexto.Children.Add(yellowTriangle);
chat.Children.Add(panelTexto);
}
}
This code works, but the Textblock.TextWrapping not.
I'm new in WP maybe this code isn't the best, if you see other error, let me.

This line is the reason:
panelTexto.Orientation = System.Windows.Controls.Orientation.Horizontal;
When you set orientation of the StackPanel to horizontal, then it will be as wide as its content. You have to change your layout, for example use Grid instead.
I have modified your code. This should work as you described:
public void ponerMensaje(string mensaje, bool me)
{
Grid panelTexto = new Grid();
Thickness marginpanel = panelTexto.Margin;
marginpanel.Bottom = 10;
panelTexto.Margin = marginpanel;
//Create the colorBrush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Yellow;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
//Create the triangle
Polygon yellowTriangle = new Polygon();
yellowTriangle.Fill = yellowBrush;
//Create the triangle's points
System.Windows.Point Point1 = new System.Windows.Point(0, 0);
System.Windows.Point Point2 = new System.Windows.Point(10, 0);
System.Windows.Point Point3;
if (!me)
Point3 = new System.Windows.Point(10, 10);
else
Point3 = new System.Windows.Point(0, 10);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
//Add the points
yellowTriangle.Points = polygonPoints;
//Create the textblock
Grid gridParaTexto = new Grid();// In WP TextBlocks haven't Backgroundcolor
gridParaTexto.Background = yellowBrush;
TextBlock texto = new TextBlock();
texto.TextWrapping = TextWrapping.Wrap;
texto.Text = mensaje;
texto.Foreground = blackBrush;
gridParaTexto.Children.Add(texto);
panelTexto.Children.Add(yellowTriangle);
panelTexto.Children.Add(gridParaTexto);
//Add the message
if (!me)
{
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = GridLength.Auto,
});
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star),
});
Grid.SetColumn(gridParaTexto, 1);
Grid.SetColumn(yellowTriangle, 0);
chat.Children.Add(panelTexto);
}
else
{
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star),
});
panelTexto.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = GridLength.Auto,
});
gridParaTexto.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
Grid.SetColumn(gridParaTexto, 0);
Grid.SetColumn(yellowTriangle, 1);
chat.Children.Add(panelTexto);
}
}
I have replaced StackPanel with Grid with two columns. One column is for yellow triangle and another one is for text.

Related

Viewing applications in WPF with errors

I have an application that breaks the image when the screen size changes
Below is the screen
Where can the mistake be? What can cause this situation? I do not even know where to look because it is not a permanent error only once and once is gone
This is how I load items with a line and number per view:
<ItemsControl ItemsSource="{Binding ConnectorsGridsHorizontal, Mode=TwoWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Function drawing 1 element:
public Grid DrawConnector1(Thickness margin, int nr, bool rotate, bool rightSide)
{
Grid grid = new Grid();
grid.Margin = margin;
grid.HorizontalAlignment = HorizontalAlignment.Left;
grid.Width = S10;
grid.Height = 128;
grid.VerticalAlignment = System.Windows.VerticalAlignment.Center;
Line line = new Line();
line.X1 = S10HALF;
line.X2 = S10HALF;
line.Y1 = 0;
line.Y2 = 128;
line.StrokeDashArray = new System.Windows.Media.DoubleCollection() { 4, 2, 1, 2 };
line.Stroke = System.Windows.Media.Brushes.Green;
line.StrokeThickness = 4;
grid.Children.Add(line);
Grid inGrid = new Grid();
inGrid.Width = S10;
inGrid.Height = HF;
inGrid.Background = System.Windows.Media.Brushes.Black;
grid.Children.Add(inGrid);
Border br = new Border();
br.BorderThickness = new Thickness(0);
br.CornerRadius = new CornerRadius(10);
br.Background = System.Windows.Media.Brushes.White;
br.Margin = new Thickness(-10, -10, -10, -10);
br.Width = 20;
br.Height = 20;
br.VerticalAlignment = rightSide ? VerticalAlignment.Top : VerticalAlignment.Bottom;
br.RenderSize = new System.Windows.Size(br.ActualWidth + 1, br.ActualHeight + 1);
if (SelectedConnector + 1 == nr) br.Background = System.Windows.Media.Brushes.Green;
else br.Background = System.Windows.Media.Brushes.White;
if (DisableGreenMark) br.Background = System.Windows.Media.Brushes.White;
TextBlock txtBlock = new TextBlock();
txtBlock.FontSize = 16;
txtBlock.Text = nr.ToString();
txtBlock.Foreground = new SolidColorBrush(Colors.Black);
txtBlock.VerticalAlignment = VerticalAlignment.Center;
txtBlock.HorizontalAlignment = HorizontalAlignment.Center;
br.Child = txtBlock;
grid.Children.Add(br);
Binding b = new Binding("S60_20");
b.Mode = BindingMode.Default;
grid.SetBinding(Canvas.TopProperty, b);
return grid;
}
GIF:
I'm also using SnapsToDevicePixels="true"
To start with, try doing this:
br.HorizontalAlignment = HorizontalAlignment.Stretch;
br.Margin = new Thickness(0, 0, 0, 0);
Do not use margin with a negative value
Stretch will provide you with an element extension
Side cut is probably caused by too narrow a strip for the element:
You have:
grid.Width = S10;
line.X1 = S10HALF;
line.X2 = S10HALF;
This indicates that you have the width set to probably 10
and Here you have set to 20
br.Width = 20;
br.Height = 20;
I also suggest changing here:
grid.Width = S10 * 2;
line.X1 = S10HALF * 2;
line.X2 = S10HALF * 2;

Using RenderedGeometry as Data of Path does not work

I want to draw a simple Path which uses RenderedGeometry of a Polygon as Data.
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
However my Canvas does not display anything.
You should force the geometry to be rendered before you it to the Canvas. You can do this by calling the Arrange and Measure methods of the Polygon:
Polygon polygon = new Polygon();
polygon.Points = new PointCollection { new Point(0, 0), new Point(0, 100), new Point(150, 150) };
polygon.Arrange(new Rect(canvas.RenderSize));
polygon.Measure(canvas.RenderSize);
var path = new Path
{
Data = polygon.RenderedGeometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Panel.SetZIndex(path, 2);
canvas.Children.Add(path);
You shouldn't be using a Polygon element to define the Geometry of a Path.
Instead directly create a PathGeometry like this:
var figure = new PathFigure
{
StartPoint = new Point(0, 0),
IsClosed = true
};
figure.Segments.Add(new PolyLineSegment
{
Points = new PointCollection { new Point(0, 100), new Point(150, 150) },
IsStroked = true
});
var geometry = new PathGeometry();
geometry.Figures.Add(figure);
var path = new Path
{
Data = geometry,
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};
Or directly create a Geometry from a string using Path Markup Syntax:
var path = new Path
{
Data = Geometry.Parse("M0,0 L0,100 150,150Z"),
Stroke = Brushes.LightBlue,
StrokeThickness = 2,
Fill = Brushes.Green,
Opacity = 0.5
};

Moving a group of elements on Canvas

I want to draw a signal image as shown below at runtime on canvas.
sample code which I used to draw this signal is as below.
Ellipse Ellipse1 = new Ellipse();
Ellipse Ellipse2 = new Ellipse();
Ellipse Ellipse3 = new Ellipse();
Line lineV = new Line();
Line lineH = new Line();
lineV.Stroke = SystemColors.WindowFrameBrush;
lineV.X1 = EndPosition.X;
lineV.Y1 = EndPosition.Y;
lineV.X2 = StartPosition.X;
lineV.Y2 = EndPosition.Y;
SolidColorBrush redBrush = new SolidColorBrush();
redBrush.Color = Colors.Black;
lineV.StrokeThickness = 2;
lineV.Stroke = redBrush;
canvas1.Children.Add(lineV);
lineH.Stroke = SystemColors.WindowFrameBrush;
lineH.X1 = StartPosition.X;
lineH.Y1 = EndPosition.Y;
lineH.X2 = StartPosition.X;
lineH.Y2 = StartPosition.Y;
redBrush.Color = Colors.Black;
lineH.StrokeThickness = 2;
lineH.Stroke = redBrush;
canvas1.Children.Add(lineH);
SolidColorBrush mySolidColorBrush1 = new SolidColorBrush();
mySolidColorBrush1.Color = Colors.Red; //FromArgb(255, 255, 255, 0);
Ellipse1.Fill = mySolidColorBrush1;
Ellipse1.StrokeThickness = 2;
Ellipse1.Stroke = Brushes.Black;
Ellipse1.Width = 30;
Ellipse1.Height = 30;
Ellipse1.Margin = new Thickness(EndPosition.X, EndPosition.Y - 15, EndPosition.X + 50, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse1);
SolidColorBrush mySolidColorBrush2 = new SolidColorBrush();
mySolidColorBrush2.Color = Colors.Green; //FromArgb(255, 255, 255, 0);
Ellipse2.Fill = mySolidColorBrush2;
Ellipse2.StrokeThickness = 2;
Ellipse2.Stroke = Brushes.Black;
Ellipse2.Width = 30;
Ellipse2.Height = 30;
Ellipse2.Margin = new Thickness(EndPosition.X + 30, EndPosition.Y - 15, EndPosition.X + 60, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse2);
SolidColorBrush mySolidColorBrush3 = new SolidColorBrush();
mySolidColorBrush3.Color = Colors.Yellow; // FromArgb(255, 255, 255, 0);
Ellipse3.Fill = mySolidColorBrush3;
Ellipse3.StrokeThickness = 2;
Ellipse3.Stroke = Brushes.Black;
Ellipse3.Width = 30;
Ellipse3.Height = 30;
Ellipse3.Margin = new Thickness(EndPosition.X + 60, EndPosition.Y - 15, EndPosition.X + 150, EndPosition.Y + 50);
canvas1.Children.Add(Ellipse3);
**Now I want the user to be able to interactively move this signal on the canvas on mouse move events.
How can I able to do this ?**
Iam using C# WPF.
If want to implement a canvas to drag elements, a DragCanvas is your choice.
Dragging Elements in a Canvas from Josh Smith and after Dragging Elements in a Canvas from igkutikov that adapt Josh Smith code. There are articles with #mustread category.
With the dragCanvas you can implement a full functionality Dragging Element Canvas, and better adapt to your code. Happy coding!

how to animate rectangle with AnimationPoint(windows phone)?

I want to animate retangle with using AnimationPoint.
How can i do it?
P.S.
I searhed in google, but did not find what i need.
I try this :
private void drawRect()
{
var d = new Grid();
var moveTranc = new TranslateTransform() { X = 1,Y = 1 };
var e2 = new Rectangle()
{
Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 0, 200, 0)),
RenderTransform = moveTranc,
RadiusX = 100,
RadiusY = 100,
RenderTransformOrigin = new Point(0.5f,0.5f),
};
var pointAnimation = new PointAnimation()
{
Duration = new Duration(TimeSpan.FromSeconds(2)),
From = new Point(e2.RadiusX,e2.RadiusY),
To = new Point(450,450)
};
var storyBoard = new Storyboard();
storyBoard.RepeatBehavior = RepeatBehavior.Forever;
storyBoard.Children.Add(pointAnimation);
Storyboard.SetTarget(pointAnimation, e2);
Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X).(TranslateTransform.Y)"));
d.Children.Add(e2);
ContentPanel.Children.Add(d);
storyBoard.Begin();
}
I found any variant of animating rectangle with using double animation
Rectangle myRectangle;
Storyboard sb;
//creating rectangle
myRectangle = new Rectangle();
myRectangle.Width = 200;
myRectangle.Height = 200;
Color myColor = Color.FromArgb(255, 255, 0, 0);
SolidColorBrush myBrush = new SolidColorBrush();
myBrush.Color = myColor;
myRectangle.Fill = myBrush;
// Create the transform
TranslateTransform moveTransform = new TranslateTransform();
moveTransform.X = 0;
moveTransform.Y = 0;
myRectangle.RenderTransform = moveTransform;
// Add the rectangle to the tree.
if (!LayoutRoot.Children.Contains(myRectangle)) LayoutRoot.Children.Add(myRectangle);
// Create a duration of 2 seconds.
Duration duration = new Duration(TimeSpan.FromSeconds(1.0));
// Create two DoubleAnimations and set their properties.
DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
sb = new Storyboard();
sb.Duration = duration;
sb.Children.Add(myDoubleAnimationX);
Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
Storyboard.SetTargetProperty(myDoubleAnimationX, new PropertyPath("X"));
DoubleAnimation myDoubleAnimationY = new DoubleAnimation();
sb.Children.Add(myDoubleAnimationY);
Storyboard.SetTarget(myDoubleAnimationY, moveTransform);
Storyboard.SetTargetProperty(myDoubleAnimationY, new PropertyPath("Y"));
myDoubleAnimationX.To = 0;
myDoubleAnimationX.From = 340;
myDoubleAnimationY.To = 0;
myDoubleAnimationY.From = 340;
// Make the Storyboard a resource.
if (!LayoutRoot.Resources.Contains("unique_id")) LayoutRoot.Resources.Add("unique_id", sb);
sb.Begin();

system.Drawing.Drawing2d.GraphicsPath

I have a c# class library that supports Silverlight4 as well.
The problem is , I need to use System.Drawing.Drawing2d.GraphicsPath in my code. for drawing a shape. but it is not there in silverlight. Can anyone suggest me an alternative?
Thanks in advance.
Regards,
James
take a look at http://www.c-sharpcorner.com/UploadFile/mahesh/PathInSL03252009005946AM/PathInSL.aspx
Snippet XAML:
<Path Stroke="Black" StrokeThickness="4"
Data="M 80,200 A 100,50 45 1 0 100,50" />
Snippet Dynamic:
public void CreateAPath()
{
// Create a blue and a black Brush
SolidColorBrush blueBrush = new SolidColorBrush();
blueBrush.Color = Colors.Blue;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Path with black brush and blue fill
Path bluePath = new Path();
bluePath.Stroke = blackBrush;
bluePath.StrokeThickness = 3;
bluePath.Fill = blueBrush;
// Create a line geometry
LineGeometry blackLineGeometry = new LineGeometry();
blackLineGeometry.StartPoint = new Point(20, 200);
blackLineGeometry.EndPoint = new Point(300, 200);
// Create an ellipse geometry
EllipseGeometry blackEllipseGeometry = new EllipseGeometry();
blackEllipseGeometry.Center = new Point(80, 150);
blackEllipseGeometry.RadiusX = 50;
blackEllipseGeometry.RadiusY = 50;
// Create a rectangle geometry
RectangleGeometry blackRectGeometry = new RectangleGeometry();
Rect rct = new Rect();
rct.X = 80;
rct.Y = 167;
rct.Width = 150;
rct.Height = 30;
blackRectGeometry.Rect = rct;
// Add all the geometries to a GeometryGroup.
GeometryGroup blueGeometryGroup = new GeometryGroup();
blueGeometryGroup.Children.Add(blackLineGeometry);
blueGeometryGroup.Children.Add(blackEllipseGeometry);
blueGeometryGroup.Children.Add(blackRectGeometry);
// Set Path.Data
bluePath.Data = blueGeometryGroup;
LayoutRoot.Children.Add(bluePath);
}

Categories