I have some textblocks included in a scroll viewer. I don't want to use the horizontal scrollbar so I came up with this idea but I don't know how to do this.
If you're referring to Screen size as the immediate window or page or just view of whatever in XAML you can easily bind the TextBlock.Width to the ActualWidth of that hosting element.
If you're talking about the actual monitor screen size then you just need to create a ViewModel with that property exposed and then bind that the same way.
If you need code let me know.
TextBlock has a property called TextWrapping. This will allow you the data in your TextBlock Not to overflow but increase the Height of TextBlock Itself. Also make sure to set VerticalAlignment to Stretch.
Related
In a WPF application, I'd like to create a textbox dynamically which will show in front of the application and be able to freely set its location by pixel. (The textbox is going to follow the mouse cursor).
This was easily done in Winforms on the fly but WPF makes things.. a little bit weird when it comes to setting a control's location by pixel since I have to add the control as a child of a container. I'm aware this is certainly doable on Canvas, but what I actually have is a dockpanel with a richtextbox to the left and a datagrid to the right.
So what are my options here? Do I have to use canvas? Can I get away with using dockpanel (or grid) to implement what I want here?
You can use a Canvas or a Grid. If you use a Canvas, set the Canvas.Left property and the Canvas.Top property. If you use a Grid, you'll need to set a size for your TextBox, set the HorizontalAlignment to Left, and VerticalAlignment to Top. To change the location of the TextBox, assign it values for MarginLeft and MarginTop.
im wondering if theres an easy way to get the width of a control in WPF at runtime while the control is collapsed.
when i use control.Width ill get the following result: -1.#IND
and control.actualWidth will return 0.0 because its collapsed.
i want to resize my window and then display the collapsed control.
thanks
Edit:
Some details
i have a grid with 2 columns in my window, the 1st column holds a tab control, the 2nd column holds an expander control. i want to extend the width of my window when expanding the expander control, so the content in the 1st column will remain its size.
Put the control in question inside a container (like a ContentControl) and collapse the container rather than the control itself. Then you should be able to simply call Measure (or use the DesiredSize property) on the control to determine how much room it wants.
What size do you expect to get?
The size is not just dependent on the control but also on its container. So the actual size can not be determined unless the control is actually rendered.
Instead of using Collapsed you could make it Invisible that way it will be sized by its own logic and the logic of the container.
EDIT
In the comments it became clear that what the reason was for needing the size of the control:
I have a grid with 2 columns in my
window, the 1st column holds a tab
control, the 2nd column a holds an
expander control. i want to extend the
width of my window when expanding the
expander control, so the content in
the 1st column will remain its size.
My answer:
Set the SizeToContent of the window to WidthAndHeight and set the width of both grid columns to auto. That should take care of it.
I believe you're going about this the wrong way. You can set the Window Width and height to "Auto" and then it will take care of all the resizing stuff.
The problem arises whenever you directly set the Width property of any control(trust me I've done it). Once you do that, you've told WPF hands off of resizing logic, I know what I'm doing.
If you think something isn't resizing at the right time you can add a handler to some event and then call control.InvalidateVisual() or control.InvalidateMeasurement() which will make it go through a whole new layout pass.
You have to call the UpdateLayout method on the control or conainer of control. After that the things may work properly.
In UWP you can determine size of collapsed control by making it visible for a sec and then hiding it again, change is not noticeable:
var oldVisibility = myBorder.Visibility;
myBorder.Visibility = Visibility.Visible;
myBorder.UpdateLayout();
var height = myBorder.RenderSize.Height;
myBorder.Visibility = oldVisibility;
Post which is marked as an answer actually does not answer the question, it just gives a workaround.
To get the size of the collapsed control you need:
Set control's visibility as Hidden (Collapsed won't evaluate).
Call Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)) method of the control.
Get the size from DesiredSize property of the control.
Then you can Collapse your control back.
In fact the UserControl lacks the property 'SizeToContent' that we have in Window.
So the question is:
what's the easiest and right way to simulate SizeToContent=WidthAndHeight behavior on UserControl?
UPD... yeah I know it suppose to get that automatically if no Height and Width defined in the container where you're placing a user control.
But it doesn't work when you placing one userControl with defined sizes, into another with no sizes, and altogether they go inside the container.
it this case your second control will take all the space it can get.
Use a Grid and set either the Row and Column height to * for the items you want to size to the window.
Just don't set the Width and Height properties. It will then take on whatever width and height its child requires.
I would like to get a Grid's actual Width (or Height) and display it in a textbox, but the width/height should be "live", when I resize the window I would like to see the new sizes.
How do I do this?
Update:
With regard to Matt's answer:
This works fine but now i would like to go a step further.
I need the actual width in a variable for testing purposes like to set some if statements or to scale other objects that are in a grid.
How do I do this?
<Grid x:Name="myGrid">...</Grid>
<TextBox Text="{Binding ActualWidth,ElementName=myGrid}" />
(The TextBox could, of course, be inside the Grid.)
The trick is that Width only specifies the initial width of a control. ActualWidth is the "live" property you should bind to.
To answer your second set of questions:
You can add a DependencyProperty to the Window and bind it to the ActualHeight of the Grid in the same way the Text property of the TextBox was bound.
It is also possible to bind other properties of other controls to the ActualHeight of the Grid. If you need a calculation inbetween use a ValueConverter to change the value to something you need.
I've created a user control using WPF and I want to add it to window. I've done that, but I can't make my control have a height higher than the height it has in its own xaml file. My MaxWidth and MaxHeight are both infinity, but I can't make the control any taller than what it is in its xaml file.
To get around this, I have to make all my user control enormous so I'll be able to size them to whatever I want. This doesn't seem right, I have to be missing something.
Removing the height and width is the way to go. The designer(blend) has some special designer width and height properties that they can use to design in, but won't set the height for runtime.
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="412" d:DesignHeight="230"
That is the xaml that will be at the top of the Window/UserControl. This should help explain things.
Why do you want your control to have a height higher than the height it has in its own XAML file? Couldn't you just remove the height in the control's XAML file, and explicitily set the height of the control when you declare it in the other XAML files (or code) that use it?
If I remove the height and width in the controls XAML file I lose the ability to use the designer for my user control. So short answer, that did solve my problem, but now I can't use the designer for user controls. Doesn't seem like I'm any better off.
The problem could be that the inner controls in your user control aren't stretching to your control. Try setting HorizontalAlignment="Stretch" or Width="Auto" on the inner controls, or you could try binding the Width property.
Ok, after some further investigation, I've misspoken. its the Grid thats causing the problem. If I set the grid Width and Heigth to Auto then everything works fine, but I lose the ability to use the designer.
I have all of the alignments set to Stretch for both the Grid and its controls.
So in summary, everything works fine if I set Grid.Width = Auto and Grid.Height = Auto, but when i do that, I lose the ability to use the designer.
I'm not aware of any width/height attributes for the VS designer if that's what you're using. I've used the MinWidth/MinHeight attributes in my xaml pretty effectively, however, to deal with the situation that I think you're describing.