I would like for the Width attribute to dynamically expand until it reaches 150, and after this, user should be able to expand it further via drag (MaxWidth won't allow this).
Width="150*"
has similar functionality, but in the opposite direction(minimum width and expand as needed). I want to have "expand as needed" and maximum expansion width.
Is there any way this can be done using XAML?
Put a line on the edge that you want the user to be able to drag. Set the Cursor property to "SizeWE" on that line so that the user knows he can drag it. From that line, handle the mouse down, mouse move, and mouse up events. In the MouseDown call CaptureMouse() from the line control and save the mouse position. On mouse move, get the new mouse position, calculate the change from the mouse down position, and increase the width of the control that you were constraining to 150 by the amount of the mouse move in the right direction and then reset the original mouse position to the current mouse position. On MouseUp, call ReleaseMouse() from the line control.
Hope this helps.
You can programmatically set the width depending on the size of the item. Bind the width to a GridLength and then you can let the user control it, or control it yourself up to 150
private GridLength _marquee1Width = new GridLength(150, GridUnitType.Pixel);
You can set this to GridUnitType.Pixel, GridUnitType.Auto, or GridUnitType.Star
Related
The questions says it all.
How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.
I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.
Can someone provide a simple example, so I can extract the idea to apply to my situation.
NOTE: Runtime of course.
Assuming you need it runtime:
You can save the control as bitmap using Control.SaveToBitmap method
Create cursor from image.
Set the current cursor which we created from control.
Once drag and drop completed reset the cursor.
Let's take a simple example of dragging a button around.
Suppose you have two types of container controls:
1) an X-Y layout
2) a flow layout (assume left to right)
When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.
As the mouse moves, first check if the mouse has changed container controls.
If so, then remove the button from its current parent and add it to the new parent.
If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).
If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.
As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.
This should give you the basic idea that you could use to start coding something.
I'm trying to show some movable controls in silverlight. I've a grid, and dynamically I've to add some controls(I'm now trying with Thumb). And user can move those controls within the grid(in the space specified for the grid). I'm not saying about the Drag and Drop controls. Actually the controls are to move as a user press mouse left button on it and starts to drag it.
Please help. Thanks in advance.
What you describe is drag & drop only. You need to implement this.
handle the left mouse click, mouse move & left mouse up events for each of the controls you want to allow to be moved.
in the left mouse click event handler:
set a flag "drag_on" to True
in the mouse move event handler
check if "drag_on" is true, if false, return.
if drag_on is true, then set the control position (x, y) same as the mouse position.
you will get the mouse position from the parameter of the event handler
in the mouse up event, set drag_on to false. Also set the control position to that of mouse position.
Note:
you will need to transform the mouse position to the grid.
Get Absolute Position of element within the window in wpf
instead of setting the control position to the mouse position, you can also try setting the mouse position as the center of the control (homework for you).
Simple question, but I didn't know what's the keywords to Google.
Suppose we want to adjust the height of the taskbar on Windows. When u place the mouse cursor at the edge of it, the cursor will change to an up and down arrow meaning that the taskbar is resizable.
How do I get that cursor to come out in C#?
Is there a control for it? Or do I have to check if the mouse is on the edge of the control, if it is change cursor. On click resize according to the difference?
You would put a splitter control onto the form, set it to horizontal, then put the control you want to be sizeable (taskbar, whatever) inside the splitter, go into the dock settings and dock it to all sides.
I want to achieve the following with a WPF application (in a certain area/defined area):
When clicking and holding on the app, you can draw a square
You can do this as many times, but not overlap any squares
You can drag squares around the application
What do I need to achieve this, I assume a bunch of onclick/onmove's. Is there an easier way, such as using canvas? Any insight would be great.
You will have to use a canvas if you want the squares to appear where the user clicks and drags.
The mouse down event would define one corner and the mouse up the second. You'd have to constrain the movement of the cursor so that the x and y dimensions of the rectangle were the same.
On each mouse move event you'd have to check if the cursor were over one of the existing squares and prevent the square growing any further.
For the dragging of existing squares modify the mouse down event to check what's under the cursor. If it's the canvas start the square drawing mode, if it's a rectangle (square) then start the dragging mode. Again you'd need to use the mouse move event to check that the square doesn't intersect with any existing squares.
There's a code project article describing how to drag elements inside a Canvas: Dragging Elements in a Canvas
Speaking of ChrisF's mentioning of using a Canvas, I would suggest you use DragCanvas (found in the article)
I have a tabcontrol with several tabpages. I also have hooked into the InputPanel.EnabledChanged event so that the tabcontrol resizes when the inputpanel is enabled. What I would like to do is ensure that the control with the focus is visible when the tabpage is resized. What's the best way to accomplish this. Note that I am using the .NET Compact Framework.
Thanks,
MrB
You have a couple of things you need to/may want to keep track of here:
• Current scroll position
• Position of the tapped element (I assume a textbox) when they tapped it
Note that the EnabledChanged event for the InputPanel is pretty easy to deal with (I assume you've already done it): check the InputPanel's Enabled state and do a += or -= to the tabcontrol's Height so the InputPanel doesn't cover anything up.
My understanding is that your issue is really to determine where the textbox was, because your worst-case scenario is that it will be moved off-screen when the tabcontrol resizes.
I believe (not 100% certain) that resizing a tabcontrol will keep the top of the tabcontrol contents visible and you'll have to scroll down to see anything that is now "covered" by the InputPanel.
What you'll want to do in the EnabledChanged event is find out the location of the control that was tapped (or the screen coordinate that was tapped - whatever is easiest for you). If it falls within the danger zone of (ClientSize.Height-InputPanel.Bounds.Height,ClientSize.Height), you'll need to adjust your scroll.
This is half-C#-half-pseudocode but you should be able to see where I'm going with it:
int yOffset = top y-coordinate of the tapped control;
if ( yOffset > ClientSize.Height-inputPanel.Bounds.Height )
{
int yShift = amount to shift everything up;
Point scrollPosition = AutoScrollPosition;
scrollPosition.Y = scrollPosition.Y - yShift;
AutoScrollPosition = scrollPosition;
}
What this does, in a nutshell, is checks if your InputPanel is going to cover up the desired control, and if so, scroll up by yShift pixels, where yShift might be the whole height of the inputPanel, or just enough to barely show your control (whatever you want).
• .NET CF seems very forgiving about setting invalid scroll rules. If you can only scroll up 10 pixels and you tell it to scroll up 100, it will stop at 10.
• Screen coordinate (0,0) is the top left of the visible screen - NOT the form being displayed. You can get tripped up if you assume that (0,0) is the top of your form.
The only way that comes to my mind is calculate this by currrent controls positions on a screen.