12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System.Drawing;
- namespace System.Windows.Forms
- {
- public class Label : Control
- {
- private System.Windows.Controls.TextBlock textBlock;
- protected new System.Windows.Controls.Label control { get; }
- public override bool AutoSize
- {
- get => base.AutoSize;
- set
- {
- if (base.AutoSize != value)
- {
- base.AutoSize = value;
- // when AutoSize is false, update the control size (case: FR LabelControl wrong scale when AutoSize is set to False after setting Width and Height)
- if (!value)
- {
- SetControlWidth(Width);
- SetControlHeight(Height);
- }
- }
- textBlock.TextWrapping = value ? TextWrapping.NoWrap : TextWrapping.Wrap;
- }
- }
- public override string Text
- {
- get => textBlock.Text;
- set
- {
- bool needFixLeft = AutoSize && control.FlowDirection == FlowDirection.RightToLeft;
- var width = needFixLeft ? Width : 0;
-
- textBlock.Text = value?.Replace("\t", "");
- base.Text = textBlock.Text;
- if (needFixLeft)
- {
- // reset WPF measure cache
- control.Measure(new Size(10000, 10000));
- Left += width - Width;
- }
- }
- }
- private ContentAlignment textAlign;
- public ContentAlignment TextAlign
- {
- get => textAlign;
- set
- {
- textAlign = value;
- Helper.GetContentAlignment(value, out System.Windows.HorizontalAlignment h, out System.Windows.VerticalAlignment v);
- control.HorizontalContentAlignment = h;
- control.VerticalContentAlignment = v;
- }
- }
- public FlatStyle FlatStyle { get; set; }
- public bool UseCompatibleTextRendering { get; set; }
- public Label()
- {
- control = new();
- SetControl(control);
- textBlock = new();
- control.Content = textBlock;
-
- // match winforms appearance
- Padding = new Padding(2, 0, 0, 0);
- textBlock.Margin = new Thickness(0);
- AutoSize = false;
- }
- }
- }
|