.NET Framework - WPF textblock autosize : a kind of magic ???

Asked By Christian Hubert-Hugoud / weabow - Xtrem7 - Groobax
17-Mar-10 05:48 AM
I use a textblock in my personnal msgbox to display texts. Because of their
changing size, I want my textblock to become autosize. So when I set its
text property, I work a little bit :

public void printText( string TextToShow)
{
tMsg.Text = TextToShow;

// set the convenient height
Double lineHeight = tMsg.LineHeight;
System.Windows.Size desiredSize = new System.Windows.Size(
tMsg.Width, 1000);
tMsg.Height = desiredSize.Height * lineHeight;
}

What I do not understand, even if it works fine :

How the desiredSize is set to the right value, when I never tell in code
that it is linked to tMsg (textblock) ?

Christian
TextBlock
(1)
WPF
(1)
Target.Height
(1)
Target.Width
(1)
TMsg.LineHeight
(1)
TextToShow
(1)
DesiredSize.Height
(1)
TMsg.Height
(1)
  Patrice replied to Christian Hubert-Hugoud / weabow - Xtrem7 - Groobax
17-Mar-10 06:39 AM
Hi,

And what if you comment the tMsg.Height= line ? In whihc container is your
textblock.

You'll find at http://msdn.microsoft.com/en-us/library/ms745058.aspx, a
description of how WPF handles the layout (especially see the "Measuring and
Arranging Children" section). If I remember you have a first pass to measure
each element recursively based on their content and then you have a second
pass that does the actual layout...

I have only a theorical knwledge of WPF but AFAIK you do not have to code for
this (for now I believe the heght you gave is just too much and WPF
calculates another actual height).

--
Patrice


écrit dans le message de groupe de discussion :
  Andy O'Neill replied to Christian Hubert-Hugoud / weabow - Xtrem7 - Groobax
17-Mar-10 07:26 AM
Presumably inside whatever container tMsg is you have your textblock.
Textblocks with no height or width set will take text and expand until they
fill their container.

Like a balloon in a box.
If you blow the balloon up, it will expand to fill the box.

So when you make it is container bigger, the textblock can expand to fill it,
if it is content needs to.
  Christian Hubert-Hugoud / weabow - Xtrem7 - Groobax replied to Christian Hubert-Hugoud / weabow - Xtrem7 - Groobax
18-Mar-10 07:00 AM
Thanks to Andy & Patrice.

In fact, the desiredSize is computed each time a controle (?) is resized.

This function runs fine :

public void printText( TextBlock Target, string TextToShow)
{
Target.Text = TextToShow;

// set the convenient height
Double lineHeight = tMsg.LineHeight;
System.Windows.Size desiredSize = new
System.Windows.Size(Target.Width, 1000);
Target.Height = desiredSize.Height * lineHeight;
}

with theses calls  (2 tetxblocks):

printText(tMsg, TextToShow);
printText(tMsg2, "TextToShow\nCoucou : ceci est la seconde ligne
? ?tre imprim?e ici !");
help
If you still find the GDI is slow to your need, maybe a migrate to WPF? Thank for your reply No speed increment, with code correction, i think that compiler optimization obtain the max speed of execution What mean, by code point of view, migrate to Wpf ? RobertoA it is a long time since I have done Windows graphics, but: Assuming you and maybe a complete-but-consice code example would help others point out the problem. WPF is a new API for developing a windows application in which the rendering is accelarated optimization you could and you feel it is the limit of GDI, then moving to WPF might help. For what it is worth, I am not aware of any research that suggests that WPF's advantages lie in the area of performance. In fact, a number of sources indicate that at least for simple graphics, WPF is significantly slower, due to its more complex layout engine. Remember: while WPF is using 3D hardware acceleration for rendering, GDI+ does use hardware acceleration as well, just is actually spent in the rendering itself. There are certain kinds of rendering for which WPF is well-suited, especially in the area of animation and transformations, but for just drawing
C# and WPF .NET Framework Just started experimenting with WPF and was wondering if someone knows how to reference properties from a WPF project in C#. Description: I have an oldish program I wrote in C#. Part of of buttons that will launch other apps. Ive created a little animated information popout in WPF that contains and image and a bit of text. Ive imported this project into the that appears in the popout will depend on which button is being moused over. The WPF layout is - Window Border StackPanel Image TextBlock All works well except I dont know to reference the Image or TextBlock from the C# part to insert the appropriate image and text (Note: I am not things Ive added dont appear. This is probably a very basic question and probably more WPF than C# but I thought I'd give it a go. mick C# Discussions UIElementCollection
I run it alone, it will just run - no exception thrown. This is contrast to WPF which always throws exception (with / without debugger). Does anyone have any idea why? C# Discussions decision, though I do not agree with it. And based on your description of the WPF behavior (I did not verify), it seems that they themselves reconsidered (though of course we almost certainly talking about different people for each feature, it seems very likely that the WPF folks made their decision knowing that the Forms folks went the other way, and understanding to educate about, than trying to provide a more elaborate solution. Now, as for why WPF went that direction I am not entirely sure. WPF primarily uses DirectX as its underlying unmanaged API which, while not thread-safe per se at least is more straight-forward about its lack of thread-safety. But, WPF is still a GUI and still relies on windows and their owner threads to deal Invoke(new Func<Label, string> (GetLabelText), label)); else { return label.Text; } } On the other hand. WPF validates if you are updating a control on the Dispatcher thread. This way the WPF control prevents unpredictable behavior. It will throw an exception so your are forced to update
WPF & Graphic .NET Framework Hello, time." this sounds interesting, so I googled for a simple graphic on a window or a form, etc. How do you do the same thing in WPF, trying to draw a line on a Canvas? I looked at http: / / msdn.microsoft.com method inside the xaml-code? Thanks Ernst C# Discussions Graphic (1) Describe (1) XAML (1) WPF (1) WPF program (1) Canvas (1) Grid (1) Neil Barnwell (1) Unfortunately, as far as I can recall there are not any WPF experts following ths newsgroup. it is really primarily a C# newsgroup, so other .NET technologies only inasmuch as they have used them themselves. That said, since Neil Barnwell is promoting WPF, perhaps he is the missing WPF expert the newsgroup has been looking for. :) In the meantime, I can with my limited experience using WPF point out that it is primarily a declarative graphics model, not imperative. That is, you
images is supposed to be simple. . . . but. . . I have a fader javascript that has a target height of 200 with a target width of 440 in pixels. Lets say I have 604 height by 795 width. Since I images is supposed to be simple. . . . but. . . I have a fader javascript that has a target height of 200 with a target width of 440 in pi