I'm using C# with WPF, to draw some graphics and texts into a WPF Canvas.
For adding texts to the canvas I use a TextBlock and this works fine when I don't change the size of the Canvas.
When I extend the width of the canvas, sometimes the text is not drawn (from the x-position where I extended the canvas). The text is there, because I can see the text background (and also a strikethrough which I added for testing).
When I change the length of the text and/or set the TextBlock length in code, the behavior changes. Meaning with some changes the text is shown.
The graphics are shown just fine in all circumstances.
I've tried to make sure the Width and ActualWidth of the Canvas are correct before I draw the texts.
I use this code to draw the text onto the Canvas:
private void drawHeartrate(string hr, double xHr)
{
TextBlock hrTextBlock = new TextBlock();
hrTextBlock.Text = hr;
hrTextBlock.Foreground = Brushes.Black;
hrTextBlock.TextDecorations = TextDecorations.Strikethrough;
hrTextBlock.Background = Brushes.Yellow;
hrTextBlock.FontFamily = new FontFamily("Soho Gothic Pro");
hrTextBlock.FontSize = 14;
Canvas.SetLeft(hrTextBlock, xHr);
Canvas.SetTop(hrTextBlock, -2);
CnvsEcg.Children.Add(hrTextBlock);
}
The screen where the texts is not displayed anymore looks like this (from the above code):
Screenshot of problem text
Related
I'm trying to add .ico 48x48 image before text in WinForms button 87x30 size:
button1.BackgroundImageLayout = ImageLayout.Stretch;
button1.BackgroundImageLayout = ImageLayout.None;
button1.BackgroundImageLayout = ImageLayout.Zoom;
button1.ImageAlign = ContentAlignment.MiddleLeft;
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleRight;
Result is:
I'm trying to figure out, how to align image on the left side with text on related distance, like this:
edit:
button1.TextImageRelation = TextImageRelation.ImageBeforeText;
button1.TextAlign = ContentAlignment.MiddleLeft; /// MiddleRight; // MiddleCenter;
button1.ImageAlign = ContentAlignment.MiddleRight; /// MiddleLeft;
Result:
The background image property is like the operating system desktop background, it is a wallpaper, that can be stretched, adapted, repeated...
Therefore here you don't need to use BackgroundImage for a button icon style image associated to its text.
If you use the Image property and set alignments to left for it and right for text, all works fine:
Then you can adapt these alignments as well as width and height to the desired result depending on the image size and/or text size and length.
Also, as indicated by the duplicate I finally found, to simply center all, you can use the TextImageRelation and set it to TextImageRelation.ImageBeforeText without changing alignments, and if necessary by increasing the height according to the size of the displayed image to have a clean result:
Am trying to change a label font size depending on screen resolution. Have tried when the form is loading, shown and also in the form constructor, but on screen the font size is the same as design time.
Rectangle resolution = Screen.PrimaryScreen.Bounds;
if (resolution.Width == 1024 && resolution.Height == 768)
{
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f);
}
I've added a double click event to the label to check the font size, and it says it's 40 in a message box (MessageBox.Show(this.labelEnterRegistration.Font.ToString());), so why doesn't the form display reflect this?
I have tried the label Invalidate()but that didn't work either.
Have fixed it. As it was done before it was setting the fonts emSize, I did the following so it changes the pixel size:
FontStyle style = this.labelEnterRegistration.Font.Style;
this.labelEnterRegistration.Font = new Font(this.labelEnterRegistration.Font.FontFamily, 40f, style, GraphicsUnit.Pixe
And now keeps the same font style as well!!
Thanks for the comments #HEPÄ°MÄ°ZYARBAYMEHMETALKANIZ, made me think about it some more.
I am trying to animate a label in a WPF application. The label gets created programatically (and dynamically) so it is not defined in XAML, but is created in the C# code.
Animation Story
The label appears in the bottom of the window. The label should be positioned lower than the window, so the user can not see it initially. Then a the label moves up (like sliding) and fades out before it reaches the top of the window.
What I have done
I have implemented this behavior myself in an other project. This time I want to use WPF which should perform better.
So far I have seen there should be multiple ways of doing this. Starting with a DoubleAnimation, going by a PathAnimation and VectorAnimation (the last of which I have not tested successfully).
Encountered problems
The animation works nice with a DoubleAnimation, but there is a problem: When I resize the window, the label gets resized too (similar to an anchor in Winforms). When I make the window smaller the label gets smaller too, until it disappears completely. This effect occurs only in the height of the label. I added the code snippet adding the label. Maybe you find some error. Also there should be a better way to implement this (I personally find it very ugly).
Label lbl = new Label()
{
Content = "Test",
FontSize = 36,
Foreground = new SolidColorBrush(Colors.Red),
Background = new SolidColorBrush(Colors.Black),
HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
VerticalAlignment = System.Windows.VerticalAlignment.Top
};
lbl.Margin = new Thickness(0, this.MainGrid.ActualHeight + lbl.ActualHeight, 0, 0);
this.MainGrid.Children.Add(lbl);
UpdateLayout();
Transform myTransform = new TranslateTransform();
lbl.RenderTransform = myTransform;
DoubleAnimation AnimationY = new DoubleAnimation((this.MainGrid.ActualHeight + 20) * -1, TimeSpan.FromSeconds(4));
myTransform.BeginAnimation(TranslateTransform.YProperty, AnimationY);
Questions
As I said, I have found multiple ways that seem to achieve the same behavior. Which one could I use to do this. I still have to do the fade-out on the top of the window, but this animation is easier to do compared to the movement.
The animation is fine. The reason your label is resizing is because you are adding it to a Grid. If you want your label to have a fixed height, then set its Height or MinHeight property.
I am using following code for animation(right to left marquee):-
private void RightToLeftMarquee(TextBlock tb)
{
doubleAnimation = new DoubleAnimation();
doubleAnimation.From = -tb.Width;
doubleAnimation.To = TickerCanvas.Width;
doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(100));
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Right)"));
sb.Children.Add(doubleAnimation);
sb.Begin(tb,true);
}
This code moves the textblock fine when width of textblock is less then the canvas but when the textblock width is greater then canvas width the marquee flickers.
The flickering you're seeing is probably due to ClearType sub-pixel positioning. You have a few options, but the animation will not look as smooth after applying them:
TextOptions.TextFormattingMode="Display"
This turns on the newer WPF 4.0 ClearType algorithm, which will make the text look crisper.
UseLayoutRounding="True"
Ensures that WPF aligns everything to the device pixels (no more blurry borders, images, etc.)
I always use both of these settings at the root elements of any app (i.e. all Windows) because it generally improves the way the application looks.
I'm trying to dynamically place content with one of the items being an image. I can get the image to load to the grid, but while the labels all align nicely, the image appears in the middle of the screen. I don't believe I'm doing this right, I'm wondering if I should be using a list? But, I'm open for suggestions. Currently I'm writing the labels with enough left margin space to separate them, but when the image renders it appears in the middle of the window.
...
Uri imguri = new Uri("/MyName;Component/Resources/myimage.png", UriKind.RelativeOrAbsolute);
BitmapImage ni = new BitmapImage(imguri);
gridEvents.Children.Add(new Label { Content = "Travel:", Margin = new Thickness(300, 0, 0, 0), FontSize = 18 });
gridEvents.Children.Add(new Image() { Source = ni, Height = 15, Width = 15 });
...
I believe the problem is similar to how I'm forcing the labels position with a hard coded margin. I'm new to wpf, so if you know of a better way - I'm ready to learn.
Image appears in the middle of the grid because you didn't set any positioning properties, like margin or align. Position in the center of the grid is just default behavior.
Actually you'd better use ItemsControl or ListView. To better understand ItemsControl read this series of articles.