How can I obtain the lenght of a string (given a font , size weight etc) in Pixels? I have seen recomendations to try System.Drawing.Graphics.* but that Assembly / Namespace doesn't seem to be available to me in silverlight.
I hope to center a text box under an image, but the text is provided dynamically.
Since your goal is to Centre the TextBox don't mess around with calculating width etc. Just tell the Container to centre the textbox.
eg.
<Grid>
<Image Source="ToolBox Avatar.png" Stretch="Fill"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="TextBlock" TextWrapping="Wrap"/>
</Grid>
Thought I not familiar with developing for Silverlight, I think that this might be of use to you.
Related
I have images of 600px width and 600px height. we have three sizes of circles. all have the center in the middle. Some have reflection as shadow beneath it. I would like to crop the image for display purposes.
So the largest circle as shown above has a diameter of about 500 pixels, but the medium and small ones have less. I know in the code which size I have of object type Product. Because of the size differences I have to place them differently and used three placeholder images for it, like this:
<Image x:Name="imgCoinHolderSmall"
HorizontalAlignment="Center"
Margin="0,495,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
<Image x:Name="imgCoinHolderMedium"
HorizontalAlignment="Center"
Margin="0,510,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
<Image x:Name="imgCoinHolderLarge"
HorizontalAlignment="Center"
Margin="0,520,0,0"
VerticalAlignment="Top"
Stretch="Fill"
Width="200"
Height="200"/>
So can I change the properties of the image such that it does not display the red part of this screenshot:
By the way, I do not display the images on their original size (as you can see at the xaml code) I set the width to 200. It is just a display thing, I do not have to store the new image. I would like to do it on the fly, preferably by setting image properties in the xaml. (for all three sizes of circles)
Is using the CroppedBitmap the best approach? http://msdn.microsoft.com/en-us/library/ms752345.aspx it is for windows rt by the way.
One option would be to use a clipping mask:
<Image Source="MyImage.jpg">
<Image.Clip>
<RectangleGeometry Rect="10,10,80,80"></RectangleGeometry>
</Image.Clip>
</Image>
The rect structure takes X,Y, Width and Height values that you have to set depending on your image.
this are many similar questions asked to this but no good answer? i use context drawing to draw input text on a certain rect size "720,576", now i need to fit the whole text in that to the maximum font size, while maintaining the rows count?
i tried to create an equation to calculate that but no use. i even tried to loop size till text is clipped but i couldn't test that condition, and i searched for a week to see something similar but no avail!
lastly i tried to use a view box which is near what i want but it wont let the textblock inside to multiline as it always re-size the width contain all text in one line.
here is what i got:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="720" Width="576">
<Viewbox >
<TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center Text="how are you doining how are you doining how are you doining how are you doining how are you doining how are you doining how are you doining how are you doining how are you doining" " />
</Viewbox>
any help would be appreciated, thanks.
i cannot reproduce the behaviour you describe with just you example copy/pasted into a new project. It does write the text on several lines; Maybe you have a default setter on one object, maybe textbox...
OK very easy : set the width on your TextBox, so your TextBox will wrap. Maybe you wanna have as width the width of the ViewBox (ancestor binding).
I've had the same issue. Here is a fix:
<Viewbox StretchDirection="DownOnly">
<Grid MaxWidth="1000" MaxHeight="100">
<TextBlock TextWrapping="Wrap" FontSize="42" VerticalAlignment="Center" Text="very very very very very very very very very very very very very very very very long line"/>
</Grid>
</Viewbox>
So with extending the text line it will be resized till it is not so small, then it will be wrapped to next line
I would like to embed an image in an Richtextbox in way that the image is sourrounded by text (should look like a newspaper article which contains some images).
That's what I have until now:
<RichTextBox>
<Paragraph>
<InlineUIContainer>
<Image Source="{Binding Image}" Width="200" Height="100" />
</InlineUIContainer>
<Run Text="{Binding Text}" />
</Paragraph>
</RichTextBox>
This code embedds the image in a way so that the first line of the run starts at the bottom right edge of the image.
But I would like to embedd it such a way:
http://i.stack.imgur.com/NVUNz.jpg
How can this be implemented for WP7.1?
Thanks.
I know this can be done, i believe you would be looking at either Margin or Padding around the image.....As the whole thing will look like an object because that's what it is.
so you want to put Padding or Margin around the image to push the text away from the object giving the appearance of space between the object. Also try Pushing the text to the right and the image to the left.
I hope this will help a little. :)
I don't think it can be done until FlowDocument will be available in Silverlight. Or somebody invents a similar control.
I would like to be able to place the word "hello" centered on a specific point. I need to do this completely in XAML without extra code. Best I can tell, all the text alignment properties/styles in XAML act on text within some bounding canvas or other element.
Since I don't know the length of the text I want to center, I can't center it using my own code.
The reason I need to solve the problem entirely in XAML is that I'm not using WPF to create the XAML, I'm writing it directly to an XML DOM. It will then be loaded into a Silverlight or WPF control for display.
In most graphic languages, including SVG, which is where my code originated, text can be aligned against a "stationary point" without a bounding box.
Any suggestions appreciated
(Yes, I know this question is old.)
The effectiveness of this solution may vary with the version of Silverlight or the .NET Framework you are using, and I haven't tried it with Silverlight for Windows Phone 7. I wrote a version for stand-alone WPF applications, and I wrote another version that also works in Silverlight.
First, the version that works in Silverlight and WPF. Please note that you will need to refactor the code a little bit if you aren't using a Canvas to provide an absolute position for the center of your TextBlock. For example, you may be using a TranslateTransform to position your text.
<Canvas>
<Canvas.Resources>
<ScaleTransform x:Key="transform" ScaleX="-1" ScaleY="-1" />
</Canvas.Resources>
<Grid RenderTransform="{StaticResource transform}" RenderTransformOrigin="-.25 -.25">
<TextBlock RenderTransform="{StaticResource transform}">
Hello!
</TextBlock>
</Grid>
</Canvas>
Second, the version that works only in WPF. It doesn't work in Silverlight because it depends on the presence of the Canvas.Right and Canvas.Bottom attached properties. UniformGrid isn't in Silverlight either, but that code could have been replaced by a regular Grid with 2 star-length rows and columns.
<Canvas>
<UniformGrid Rows="2" Columns="2"
DataContext="{Binding ElementName=textBox1}"
Width="{Binding Path=ActualWidth}"
Height="{Binding Path=ActualHeight}">
<Canvas>
<TextBlock Name="textBox1" Canvas.Right="0" Canvas.Bottom="0">
Hello!
</TextBlock>
</Canvas>
</UniformGrid>
</Canvas>
By the way, there may be more efficient ways to solve this problem available. I am making no guarantees!
So I'm coming at WPF from a HTML perspective.
I just want to put a TextBox on my Window like this:
<Grid>
<TextBox Name="theName" />
</Grid>
Turns out that the TextBox is then HUGE, covers the whole window. (!)
Ok, that's not what I want, but I don't want to define the EXACT size either since I know Height and Width should be flexible, so I try:
<TextBox Name="theName" Width="Auto" Height="Auto"/>
Same thing. So I try:
<TextBox Name="theName"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
Same thing. So I just hard code the sizes:
<TextBox Name="theName" Width="100" Height="20"/>
Which I know is not a good programming practice in WPF.
So, what how do you tell TextBox to "display default sizes for the font size being used"?
You can take Bryan's example even a bit further. By specifying a specific alignment that isn't stretch and further constrain the TextBox so that it won't expand beyond a certain size. eg:
<Grid x:Name="LayoutRoot">
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Text="TextBox" TextWrapping="Wrap"
MinWidth="15" MinHeight="20" MaxWidth="500" MaxHeight="50"/>
</Grid>
You can take it even further by setting up rows/columns inside the Grid and constraining them in various fashions. As you're coming from an HTML background, think of it like using a table to control layout. Remember that you can also nest other container objects (i.e. StackPanels, WrapPanels, other Grids, etc...).
The challenge with XAML and the WPF/Silverlight controls is that they a very flexible, so you've got to get a handle on all the options and how they affect layout.
Good luck. I'm going through this exact same thing now.
Use a different container.
The Grid always streches its child controls to fill the grid cell.
You could use e.g. a stackpanel which only streches its controls in one direction.
In addition to using a different panel as Stefan mentioned you could just give the TextBox an alignment that isn't Stretch. e.g.
<TextBox Name="theName" HorizontalAlignment="Left" VerticalAlignment="Top"/>
The sizes in WPF aren't pixels, they are "device independent pixels" that are 1/96 of an inch - so in today's normal DPI setup they map 1:1 to pixels.
But, if you run the program in high DPI mode the TextBox will grow with the DPI (and the font).
So setting an hard-coded size isn't that bad.
Other than that you can only use HorizontalAlignment and VerticalAlignment that are not "Stretch", this will size the TextBox to content - but then an empty TextBox will be tiny.
You can set VerticalAlignment to "Center", "Top" or "Bottom" to get automatic height of about one line (maybe backed up by a MinHeight setting to avoid problems really tiny fonts) and then set the Width so the TextBox width does not change as the user types into it.