I am trying to create resizable textboxes (so when the window is resized the textbox adjusts itself) programatically but the text inside the textbox is always very small in comparison to the textbox:
TextBox textBox = new TextBox();
textBox.Name = name;
textBox.Text = text;
textBox.SetValue(Grid.ColumnProperty, column);
textBox.SetValue(Grid.RowProperty, row);
textBox.SetValue(Grid.ColumnSpanProperty, columnspan);
textBox.SetValue(Grid.RowSpanProperty, rowspan);
So i added a binding to keep the text at the same size as the textbox:
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
b.Path = new PropertyPath(TextBox.ActualHeightProperty);
textBox.SetBinding(TextBox.FontSizeProperty, b);
but when i do this the text becomes way too big for the textbox.
What am i doing wrong/missing here?
You are binding the fontsize which is pixels to actual height which is in device independent units so its apples to oranges.
Is there any reason to not use a viewbox?
Something like (I'm doing this freehand so beware..no ide may have typos)
TextBox textBox = new TextBox();
Viewbox vb = new Viewbox();
vb.Child = textbox;
vb.Stretch = Uniform;
textBox.Name = name;
textBox.Text = text;
vb.SetValue(Grid.ColumnProperty, column);
vb.SetValue(Grid.RowProperty, row);
vb.SetValue(Grid.ColumnSpanProperty, columnspan);
vb.SetValue(Grid.RowSpanProperty, rowspan);
Related
Is it possible to set for each side of a border its own EventHandler for mouse-enter or mouse-leave event. For example for the Left-Border of a Grid and Top-Border of a Grid?
What I am actually trying to do is allow the user to resize Grid-Elements inside a Canvas that contain a TextBlock with the mouse.
I am inserting my Grid/Border into the Canvas with the following code:
Border border = new Border();
border.BorderThickness = new Thickness(2);
border.BorderBrush = Brushes.Black;
TextBlock tb = new TextBlock();
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextWrapping = TextWrapping.Wrap;
tb.Padding = new Thickness(5, 5, 5, 5);
tb.Text = fd.LabelText;
Grid grid = new Grid();
grid.Background = labelBackgroundBrush;
grid.Background.Opacity = myOpactiy;
border.DataContext = fd;
grid.Children.Add(tb);
border.Child = grid;
I found a good example at csharphelper.com . Although my implementation is still buggy this was a good inspiration for me. Maybe it can help others who want to do the same.
Hello I have a textblock within a grid row. The text outside the width of the row is not shown. How can i split the text and display it underneath?
//Actual -->
//Date: 06/06/2018 realy long text is displayed, b
//Wanted -->
//Date: 06/06/2018 realy long text is displayed,
//but not showing everything due to width
//isue
Grid DynamicGrid = new Grid();
DynamicGrid.Width = 450;
DynamicGrid.Height = 1000;
DynamicGrid.HorizontalAlignment = HorizontalAlignment.Left;
DynamicGrid.VerticalAlignment = VerticalAlignment.Top;
DynamicGrid.ShowGridLines = false;
RowDefinition gridRow;
gridRow = new RowDefinition();
gridRow.Height = new GridLength(50);
DynamicGrid.RowDefinitions.Add(gridRow);
TextBlock txtBlock1 = new TextBlock();
txtBlock1.Text = "06/06/2018 realy long text is displayed, but not showing everything due to width isue";
txtBlock1.FontSize = 20;
txtBlock1.VerticalAlignment = VerticalAlignment.Top;
Grid.SetRow(txtBlock1, 0);
Grid.SetColumn(txtBlock1, 0);
DynamicGrid.Children.Add(txtBlock1);
Setting the TextWrapping property of the TextBlock to wrap will cause the textblock to show full text.
I have the problem that the text inside my panel gets cut of strangely. The panel is located inside a textbox. But even if I replace the textbox by a flowlayoutpanel, I have the same issue.
Code:
List<string> list = datenbank.FerienAuswahl(monat, jahr);
int i = 0;
//Create Panel
try
{
//Fill Panel
do
{
Label panel = new Label();
panel.Name = "panel" + i;
panel.Height = 30;
panel.Width = 400;
panel.AutoSize = false;
panel.TextAlign = ContentAlignment.MiddleCenter;
panel.ForeColor = Color.Black;
panel.Text = list[i];
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
i++;
} while (i < list.Count);
}
catch { }
Result:
I have already tried to change the width of the panel. But as result I only get a messed up alignment of the text.
The only settings of the textbox I have changed are these:
Multiline: True
TextAlign: Center
Size: 359; 125
Does Someone know what else I could try ?
These lines worry me:
Label panel = new Label();
Label ferien = new Label();
panel.Controls.Add(ferien);
tbFerien.Controls.Add(panel);
It seems to me you are adding one label to another. That's not good. Use a Panel or TableLayoutPanel instead of the actual panel and make sure you have your positioning good.
string data = "test";
TextBox tb = (TextBox)data; // i want something like that in order to
tb.Backcolor = color.black; // do this line
Use TextBox.Text = "bla" instead of cast
Providing that you call the code with a method of a Form, you want something like that:
String data = "test";
// creation of a new TextBox with Text assigned to data...
TextBox tb = new TextBox() {
Parent = this, // <- text box is on the Form...
Location = new Point(10, 10),
Size = new Size(100, 20),
Text = data, // <- TEXT of the TextBox is data
BackColor = Color.Black,
ForeColor = Color.White,
};
String data = "test";
TextBox tb = new TextBox();
tb.text = data;
You can assign string directly to the textbox's using text attribute.
Hope this helps.
I'm making a C# project with WPF and Visual Studio.
I'm stuck at trying to drag/move a TextBox around on my canvas in run-time.
I got the idea that, if you make a border thickness big enough, you could use the border for some sort of focus and then make an EventHandler for the border?
private Canvas DrawBox(ClassBox box)
{
Canvas myCV = new Canvas();
TextBox box1 = new TextBox();
box1.Background = new SolidColorBrush(Colors.Blue);
box1.BorderThickness = new System.Windows.Thickness(5);
box1.Foreground = new SolidColorBrush(Colors.White);
box1.MinWidth = 30;
box1.TextWrapping = TextWrapping.Wrap;
box1.AcceptsReturn = true;
myCV.Children.Add(box1);
Canvas.SetLeft(box1, box.Left);
Canvas.SetRight(box1, box.Right);
Canvas.SetTop(box1, box.Top);
return myCV;
}