how to add text in rectangle with code behind wpf - c#

I want to add textblock or label inside rectangle which is creating with code behind
Can anyone help me?
for (int i = 0; i < _RoomX.Count; i++)
{
_RoomX[i] = (Convert.ToDouble(_RoomX[i]) * 20).ToString();
_RoomY[i] = (Convert.ToDouble(_RoomY[i]) * 20).ToString();
var rectangle = new Rectangle()
{
Stroke = Brushes.Black,
Fill = brush,
Width = Convert.ToDouble(_RoomX[i]),
Height = Convert.ToDouble(_RoomY[i]),
Margin = new Thickness(
left: 15,
top: 50,
right: 0,
bottom: 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
mainCanvas.Children.Add(rectangle);
}

well rectangle is not a content control it is derived from shape object... we cannot use it as a panel.
instead of rectangle you can use a border..
if your requirement demands a Rectangle then what you can do is... create a Grid> then add rectangle to that Grid> and create a textblock and add it to same grid... since grid is not physically visible it appears like text added to rectangle ..
i will try to post a sample
Edit:
following code will help you to understand it better
for (int i = 0; i < _RoomX.Count; i++)
{
_RoomX[i] = (Convert.ToDouble(_RoomX[i]) * 20).ToString();
_RoomY[i] = (Convert.ToDouble(_RoomY[i]) * 20).ToString();
var rectangle = new Rectangle()
{
Stroke = Brushes.Black,
Fill = brush,
Width = Convert.ToDouble(_RoomX[i]),
Height = Convert.ToDouble(_RoomY[i]),
Margin = new Thickness(
left: 15,
top: 50,
right: 0,
bottom: 0),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top
};
Grid grid = new Grid();
grid.Children.Add(rectangle);
TextBlock textblock = new TextBlock();
textblock.Text = "Text to add";
grid.Children.Add(textblock);
mainCanvas.Children.Add(grid);
}

Related

Why is the Xamarin.Forms.Shapes.Path cut off when rotated, as if the shape's borders aren't rotated with the shape?

Why is the Xamarin.Forms.Shapes.Path cut off when rotated, as if the shape's borders aren't rotated with the shape? I create AbsoluteLayout, then create several shapes and add them to AbsoluteLayout.Chidrens. When the shapes do not have rotation, everything is fine, and when there is, then the shapes is cropped as in the image.
Code example
AbsoluteLayout AL = new AbsoluteLayout();
RotateTransform RT = new RotateTransform { CenterX = SomeX, CenterY = SomeY, Angle = 20 };
EllipseGeometry CircleGeom = new EllipseGeometry { Center = new Point(SomeX, SomeY), RadiusX = 50, RadiusY = 50 };
Path CircleGeomPath = new Path { Data = CircleGeom, StrokeThickness = 1, Stroke = Color.Black, Fill = Color.White, RenderTransform = RT };
AbsoluteLayout.SetLayoutFlags(CircleGeomPath, AbsoluteLayoutFlags.None);
AL.Children.Add(CircleGeomPath);
RectangleGeometry RectGeom = new RectangleGeometry { Rect = new Rect { X = SomeX, Y = SomeY - 25, Width = 150, Height = 50 } };
Path RectPath = new Path { Data = RectGeom, StrokeThickness = 1, Stroke = Color.Black, Fill = Color.White, RenderTransform = RT };
AbsoluteLayout.SetLayoutFlags(RectPath, AbsoluteLayoutFlags.None);
AL.Children.Add(RectPath);
What needs to be done so that the shapes are rotated and not cut off?
image example - shapes with and without rotation
I had tried different layouts and angles with your code on my device. Finally, I think this problem is caused by the system which will set size of the view's area on Android. So when the view rotates, a part of it will be cut off when the part is out of range.
So you can set the path's heightrequest and weightrequest to give it a bigger size of area. Such as:
Path CircleGeomPath = new Path { Data = CircleGeom, StrokeThickness = 1, Stroke = Color.Black, Fill = Color.Blue, RenderTransform = RT,HeightRequest= 200,WidthRequest =400 };
Path RectPath = new Path { Data = RectGeom, StrokeThickness = 1, Stroke = Color.Black, Fill = Color.Blue, RenderTransform = RT, HeightRequest = 400, WidthRequest = 400 };

align image to the right inside a button programmatically WPF

I'm trying to align a image inside a button to the right. however it does not seem to do what i want it to do. i have been trying for several hours now, so i think it is time to ask here. this is my code atm.
public class DirectoryButton : Button
{
public DirectoryButton(string name, string content)
{
Name = name.Replace(" ", "");
//Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative)) + content;
//Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
var img = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
var sp = new UniformGrid
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Columns = 3,
Rows = 1
};
sp.Children.Add(new Label());
sp.Children.Add(new Label { Content = content, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });
sp.Children.Add(new Image { Source = img, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center});
//sp.Orientation = Orientation.Horizontal;
Content = sp;
//Content = content;
Margin = new Thickness(0, 5, 0, 5);
Height = 65;
Width = 350;
FontWeight = FontWeights.Bold;
Foreground = new SolidColorBrush(Colors.Black);
Background = new SolidColorBrush(Colors.Gray);
HorizontalContentAlignment = HorizontalAlignment.Center;
VerticalAlignment = VerticalAlignment.Center;
Visibility = Visibility.Visible;
}
}
button Image
this makes the image in the button appear but it is not to the right. i cannot really do anything in the xaml since everything is created programmaticaly.
Regards,
Bjorn
Set the HorizontalContentAlignment to Stretch:
public class DirectoryButton : Button
{
public DirectoryButton(string name, string content)
{
Name = name.Replace(" ", "");
//Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative)) + content;
//Content = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
var img = new BitmapImage(new Uri("../Resources/folder-icon.png", UriKind.Relative));
var sp = new UniformGrid
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Columns = 3,
Rows = 1
};
sp.Children.Add(new Label());
sp.Children.Add(new Label { Content = content, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center });
sp.Children.Add(new Image { Source = img, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center });
//sp.Orientation = Orientation.Horizontal;
Content = sp;
//Content = content;
Margin = new Thickness(0, 5, 0, 5);
Height = 65;
Width = 350;
FontWeight = FontWeights.Bold;
Foreground = new SolidColorBrush(Colors.Black);
Background = new SolidColorBrush(Colors.Gray);
HorizontalContentAlignment = HorizontalAlignment.Stretch; //<--
VerticalAlignment = VerticalAlignment.Center;
Visibility = Visibility.Visible;
}
This should make the UniformGrid stretch to fill the entire Button and the Image will then end up to the right.
As I tried to explain in the comments above.
The code you've provide us works like it should.
You create an UniformGrid with three cells and add three childs to the grid.
Each cell of the grid will have the same size.
Your empty label will got to the left one. The next label to the center.
You will add your image to the right cell and set HorizontalAlignment = HorizontalAlignment.Right. This will align your image within his cell at the right edge. And also to the right edge of your grid.
Now you add your UniformGrid as Content to your button.
With the following line of code
HorizontalContentAlignment = HorizontalAlignment.Center;
you will center your grid within the button.
That's why your image is not at the right edge of your button.
As already suggested in my comment you can set
HorizontalContentAlignment = HorizontalAlignment.Right;
and your Image and the whole grid will move to the right edge, but this means also that your centered Label will also move to the right.
If you want to let the second Label stay within the center of the Button take the Answer provided by mm8 and use
HorizontalContentAlignment = HorizontalAlignment.Stretch;

WPF DocumentViewer show document as byte array

This might be a silly question, but I was wondering if it's possible to display a document in WPF's DocumentViewer control from a byte array.
If not, could someone provide an example of how the control is normally used to display a document? I can't seem to find a decent example.
It is about arranging different UIElements:
FixedDocument fixedDocument = new FixedDocument();
DocumentViewer dv = new DocumentViewer() { Document = fixedDocument };
this.Content = dv;
var page1 = new FixedPage() { Width = 600, Height = 800 };
PageContent page1Content = new PageContent() { Child = page1 };
var sp = new StackPanel();
sp.Children.Add(new TextBlock
{
Text = "Title",
FontSize = 30,
Margin = new Thickness(100, 50, 0, 70)
});
sp.Children.Add(new TextBlock
{
Text = "The quick brown fox jumps over the lazy dog...",
FontSize = 15,
Margin = new Thickness(10, 0, 0, 10)
});
Rectangle rect = new Rectangle();
rect.Width = 150;
rect.Height = 150;
rect.Fill = Brushes.Black;
sp.Children.Add(new Rectangle
{
Width = 150,
Height = 150,
Fill = Brushes.Black
});
page1.Children.Add(sp);
fixedDocument.Pages.Add(page1Content);

Rotated textblock doesn't have the correct length

I am trying to fill a Grid in a WPF application dynamically.
I'm creating a row and then some columns. in each column I am adding a textblock, like this:
GrdMainGrid.RowDefinitions.Add(new RowDefinition(Height = new GridLength(150, GridUnitType.Pixel);
GrdMainGrid.RowDefinitions.Add(new RowDefinition(Height = new GridLength(1, GridUnitType.Star);
for(var i=0; i<4; i++)
{
GrdMainGrid.ColumnDefinitions.Add(new ColumnDefinition {With = new GridLength(25, GridUnitType.Pixel)});
}
var header = new TextBlock {Text = "Header1", RenderTransform = new RotateTransform(-90), Width = 150, Margin = new Thickness(0), VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left};
.
.
.
Grid.SetColumn(header, 0);
Grid.SetRow(header, 0);
.
.
.
GrdMainGrid.Children.Add(header);
The textblock is rotated -90 degree.
While the size of the column is set to 25 pixel i doesn't show all text, when I increase the size of the column the text in textblock is also increased. see pic
I could understand this if the textblock wasn't rotated and it didn't fit in the column. But what has it to do with the size of the column when it's rotated.
And is it possible somehow to decrease the width of the column without decreasing size of text ?
Tnx in advance.
Use a LayoutTransform instead of a RenderTransform.
var header = new TextBlock { Text = "Header1", LayoutTransform = new RotateTransform(-90), Width = 150, Margin = new Thickness(0), VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Left };

How to force text into a specific width?

I use a GridView to display my layout. To this GridView i manually add some RowDefinitions and in this RowDefinitions I add 1 Canvas containing 2 rectangles:
foreach (Method m in sourceFile.getMethods())
{
if (!m.getName().StartsWith("<") && !m.getName().EndsWith(">"))
{
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
MethodsContainer.RowDefinitions.Add(row);
Canvas c = new Canvas();
c.Width = width;
c.Height = height;
c.Tag = m;
Contacts.AddPreviewContactDownHandler(c, new ContactEventHandler(onContactDown));
Rectangle r1 = new Rectangle();
r1.Height = height;
r1.Width = m.getLoc() * (width / 1000);
Canvas.SetLeft(r1, 0);
Canvas.SetLeft(r1, 0);
r1.Fill = Brushes.Red;
Rectangle r2 = new Rectangle();
r2.Height = height;
r2.Width = width - r1.Width;
Canvas.SetTop(r2, 0);
Canvas.SetLeft(r2, r1.Width);
r2.Fill = Brushes.Blue;
c.Children.Add(r1);
c.Children.Add(r2);
Grid.SetRow(c, rowCounter);
MethodsContainer.Children.Add(c);
rowCounter++;
}
}
The canvas is 200px width and 30px height. Both rectangles fill the Canvas exactly. Now i want to add some text over this both Rectangles. But I don't know how long the text is. However I want to force that the text is always printed into this 200px. How can I achieve that?
Sounds like you could make use of a ViewBox. This will make your text stretch both horizontally and vertically. I assume that's what you want if I understood the question correctly. Example in xaml
<Canvas Width="200"
Height="30">
<Viewbox Width="200"
Height="30">
<TextBlock Text="Text that will fit in 200 Width"/>
</Viewbox>
</Canvas>
And in code behind it will be like
TextBlock textBlock = new TextBlock();
textBlock.Text = "Text that will fit in 200 Width";
Viewbox viewBox = new Viewbox();
viewBox.Width = width;
viewBox.Height = height;
viewBox.Child = textBlock;
c.Children.Add(viewBox);
var txt = new TextBlock();
txt.MaxWidth = 200;
// optionally you might want the text to wrap if too long
txt.TextWrapping = TextWrapping.Wrap;
By the way, it's not pixels in WPF, it's a device-independent unit (1/96th inch) measurement.

Categories

Resources