Width for wpf Textblock inline elements - c#

Is it possible to give width for a textblock inline element in WPF? I am using Run to add inline elements to text block. I want each run to be of same fixed size.But I couldn't find width property for Run.
For example
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new Run() { Text = "abc" });
txtblck.Inlines.Add(new Run() { Text = "def" });
I want both the Runs to be of same width. Is it possible? Please help me.
Thanks in advance.

You can use InlineUIContainer:
TextBlock txtblck = new TextBlock();
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "abc",
Width = 100
}
});
txtblck.Inlines.Add(new InlineUIContainer
{
Child = new TextBlock
{
Text = "def",
Width = 100
}
});

Related

data not showing completely inside panel

Hi I'm working on Windows application and I'm adding data inside the panel dynamically however data is not showing it completely inside the panel because of font size but I don't want to resize font.
tblLayoutModule.Controls.Add(new Label() {
Text = string.Concat(pModuleName, " ", pVersion),
Padding = new Padding() { Top = 2 },
UseMnemonic = false }
);
Try to add autosize = true could work for you
tblLayoutModule.Controls.Add(
new Label() {
Autosize = true,
Text = string.Concat(pModuleName,"",pVersion),
Padding = new Padding() { Top = 2 },
UseMnemonic = false }
);

How do I set event methods for programmatically created combo boxes?

I am creating small program that has dynamically created combo boxes. Each time the user opens the program, based on some context, there could be 3-30 items that need 4 drop down lists for selection. I am creating these based off of the following code, which is just a snippet.
for (int i = 0; i < 4; i++)
{
s.Children.Add(new ComboBox()
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(m, -25, 0, 0),
Width = 75,
Height = 25,
FontSize = 12,
Name = "obj1_" + i.ToString(),
ItemsSource = objs,
});
m = m + 50;
s.Children.Add(new Label()
{
VerticalAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
Margin = new Thickness(m, -25, 0, 0),
Width = 25,
Height = 25,
FontSize = 12,
Name = "lbl1_" + i.ToString(),
});
}
s is a stack panel that I am adding each of the combo boxes too. The ItemSource is from a small method elsewhere to figure out which list should go into the drop down.
My question is, how do I call the events for these created combo boxes? Trying
private void obj1_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox this_box = (ComboBox)sender;
lbl1_1.Content = "!!!";
}
works well enough but the label doesn't exist in the current context.
Also, am I creating the boxes and labels the best way for this type of scenario?
Thanks in advance.
Inside you for loop
1)Create an new Panel
2)Add the label and combo to that panel
3)Add the newly created panel to s
Inside your obj1_1_SelectionChanged event:
1)Find the Parent control of the ComboBox
2)Search for the label inside its Children and update its text
Solution 2
When you create your controls create a Guid (or an int) and set the Tag property of your controls to that object.
Now when you are on a combo you can search your Window for the label with the same Tag
for (int i = 0; i < 4; i++)
{
Guid g = Guid.NewGuid();
s.Children.Add(new ComboBox()
{
Tag = g
});
s.Children.Add(new Label()
{
Tag = g
});
}

Set font-height to half the textblock height

I am trying to set the font-height to half the size of the TextBlock. The TextBlock is in a Grid ie. in a row of the Grid.
The grid row spans multiple rows.
I have tried.
Textblock t = new TextBlock();
t.LineHeight = t.ActualHeight/2;
But the ActualHeight is always 0.
ActualHeight is only calculated after the element is loaded. To get the size of the TextBlock before being loaded into the visual tree, you can call the Measure() method like this:
var t = new TextBlock();
var infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
t.Text = "Something";
t.FontSize = 12;
t.Measure(infiniteSize);
t.LineHeight = t.DesiredSize.Height / 2;
The infiniteSize variable tells the Measure() method to give you the optimal size of the element assuming you have infinite space to draw the element.
Alternate Solution
You can tap into TextBlock.SizeChanged event and update the line-height.
var t = new TextBlock();
t.SizeChanged += (sender, args) =>
{
t.LineHeight = t.ActualHeight / 2;
};

Get largest width of a textblock in a stackpanel

On a button click, I am reading through a CSV file, replacing ',' with '\t' and writing it out to a stackpanel.
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < r.variables.Count; i++)
{
_people.Add(new TextBlock() { Text = r.variables[i], HorizontalAlignment = System.Windows.HorizontalAlignment.Right });//.ToString() });
StackPanel stp = new StackPanel() { Orientation = Orientation.Vertical };
TextBlock tb = new TextBlock() {Text = r.variables[i]};
stp.Children.Add(tb);
_secondStack.Children.Add(stp);
}
foreach (StackPanel sp in _secondStack.Children)
{
foreach (TextBlock tb in sp.Children)
{
Size desiredSize = new Size();
tb.Measure(this.availableSize);
desiredSize = tb.DesiredSize;
}
}
}
From the file, some contain strings that are longer than others, thus the TextBlocks in the header are wider than those TextBlocks below.
How do I get the width of the widest TextBlock in the embedded StackPanel and set the width of all TextBlocks inside the embedded StackPanel to that?
Once the StackPanel is populated, this is how you get the largest Width and set it for all other TextBoxes:
double largestWidth = stackPanel.Children.OfType<TextBox>().OrderByDescending(
textbox => textbox.ActualWidth).First().ActualWidth;
foreach (var textBox in stackPanel.Children.OfType<TextBox>())
{
textBox.ActualWidth = largestWidth;
}

Adding controls to GroupBox during runtime

I'm trying to create a GroupBox, add a Grid (or StackPanel) to it then put some TextBlocks on it, all during runtime. This is what i've tried
GroupBox groupBox1 = new GroupBox();
Grid grid1 = new Grid();
groupBox1.Width = 85;
groupBox1.Height = 60;
grid1.Height = 85;
grid1.Width = 60;
groupBox1.Content = grid1.Children.Add(textBlock1);
groupBox1.Margin = new Thickness(50, 50, 0, 0);
mainWindow.canvas.Children.Add(groupBox1);
But all I get is a groupbox with a thick white border with nothing in it.
As far as I can see a Grid.Children.Add returns an int and that's not what you want to set the content of the groupBox1 to.
An untested idea from me as a non WPF expert is to set the grid as the Content of your groupbox.
grid1.Children.Add(textBlock1);
groupBox1.Content = grid1;
For simple checkboxes i used this code :
var container = new FlowLayoutPanel
{
FlowDirection = FlowDirection.TopDown,
Dock = DockStyle.Fill
};
myGroupBox.Controls.Add(container);
foreach (var myText in textList)
{
var checkBox = new CheckBox
{
Text = myText
};
container.Controls.Add(checkBox);
}
Of course the foreach statement is just for the example :)

Categories

Resources