I'm making a custom dropdown button (since the one included in wpf requires too much hacking to style right). Now that i got the button bit out of the way i need to add the drop down part.
My first thought was to add a stackpanel and use that to contain the items but it gets cut off if it leaves the borders of the grid that the button is in. Next up was the popup primitive, it gets on top of everything nicely enough but position wise it just free floats and i haven't figured out how to make it follow the button it was spawned by. I also tried using contextmenu but that seems to have no positioning controls at all and just sits where the mouse made it..
Anyways wpf is a big package and I'm just getting into it, anybody know which direction i might find what I'm looking for?
Preferred approach normally is to use a Popup. You got two very important properties with a Popup
PlacementTarget and Placement
Setup a binding for PlacementTarget on the Popup to your custom Button and then use Placement to position the Popup accordingly w.r.t to the PlacementTarget(Button)
Placement accepts an enum of type PlacementMode which gives you quite a few options to position the Popup.
Related
I am customizing the tooltip on my user control so that it looks like a callout. The tooltip is set to appear above the control (Placement=Top) with an arrow pointing down toward it.
The problem is when the control is near the edge of the screen, the tooltip is not aligned to the left-side of the control. As a result, the arrow points to an empty space to the left of the control.
Even worse, if the control is near the top of the screen, the tooltip shows up below the target control and the arrow pointing down.
Any idea how to resolve this? I am looking for some way to programmatically create the tooltip on the fly or set a trigger to select the Style based on some properties in the tooltip but I don't know how.
I have faced the same problem in the past and what I did was get the position of the PlacementTarget via PointToScreen(new Point(0,0)) and get the position of the ToolTip by calling the same method and then doing the math to figure out whether the ToolTip is correctly positioned or not. If it wasn't, then I changed the Placement of the ToolTip according to whether it was showing up to the left, right, above or below the PlacementTarget. Also, keep in mind, that PointToScreen() will only work once the ToolTip has rendered so attach a handler to ToolTip.Open and do it in there. Hope this helped. Let me know if you come across a more efficient/better way to solve this problem.
I want a button that
Displays an image with NO border, NO background, NO text
If I tab into the imagebutton, THEN it shows the background and border
Also if I hover over it, it shows the background and border
I've searched and I've tried so many different things, but nothing it exactly what I want. I've tried setting various properties on the button to make the background and border transparent, but it still shows up. I've tried a style with a custom control template. I'd rather not have to completely reinvent all the triggers etc to get the button to render on mouse over. The biggest problem with custom control template is that then I loose all existing functionality and I'm basically building a new control from the ground up.
Here is another link that came closest to what I wanted but it doesn't properly work for me.
How do you completely remove the button border in wpf? - BUT.... for some reason the hover effect gets stuck. One I mouse over the image and the button border draws, it stays stuck on until I click somewhere else.
Actually, you will want to override the control template. You're not "losing" any functionality (aside from the UI triggers).
Original/Default Template -- This is a good starting point... copy/paste that into you're XAML (wherever you want to style this button... ie Button resources, UserControl/Window resources, App Resources?). From there make your adjustments.
Another easy way is to use Expression Blend. You can easily create a new template based on the existing template, and the styling/authoring tools it provides are much better than hand-coding XAML (unless you're good at doing that).
As far as displaying an image instead of text, just set the image as the content. A Button is a type of ContentControl which means that it can house any type of content (Object).
i want to write some control that will contain 2 button and listView.
Pressing button 1 will scroll the listview up.
Pressing button 2 will scroll the listview down.
The direct scroll of the listview will be unavailable - ( will be invisible ? ).
I don't find the listview method 'scroll-up' / 'scroll-down' that i could callon the button event.
How can i make the listview scroll to be always visible ?
Someone can help me here ?
Thanks.
You have two options here, one is easier than the other.
First option (the easier, but slightly hacky way): Using the VisualTreeHelper, get a reference to the ScrollViewer in the ListView's ControlTemplate. Then you can use the LineUp and LineDown methods to scroll the content up and down and the static SetVerticalScrollBarVisibility method to hide the scrollbar. I personally wouldn't use this approach because I don't like relying on the Visual Tree which can change.
Second option (a bit harder, but not too bad if you know how): Write a new Control Template for the ListView (might need to alter the templates for it's ScrollViewer + ScrollBar), adding in two buttons that call the ScrollBar.LineUpCommand and ScrollBar.LineDownCommand. If you want to do this, I'd suggest getting a copy of ShowMeTheTemplate, then you can just copy and paste the original(s) and modify.
Hope this helps point you in the right direction.
i am trying to create a wpf app and have different parts in user controls.
in the navigation i have some buttons (now using the ribbon ctp). is it possible to change the main user control when different buttons are pressed in xaml. or is this just a bad way to do things?
sorry, really new to xaml and im trying to get my head arround it.
Further to what Carlo has said,
The way we do it is to have a blank grid in the place you want your controls to all appear and then use BlankGrid.Children.Clear() and BlankGrid.Children.Add() to set up which control is visible in this position.
We found that was the nicest programatically as we have a large number of custom controls, but Carlo's method would work nicely if you wanted to use the designer.
I think this is a pretty regular procedure in WPF. In my experience, me and other programmers put the controls where we want to show them and make their visibility hidden, collapsed or visible depending on what we want to show the user.
N.B THIS QUESTION HAS BEEN UPDATED, READ FURTHER DOWN
Hi,
I want to create a custom context menu that has 4 sub-menus, each in their own quadrant (top left, top right, bottom left, bottom right). Similar to 3ds Studio Max.
This is how I've approached the issue so far:
- create a custom WPF control, derive from ContextMenu[1]
- declare 4 dependency properties of ContextMenu, these will be menus displayed and can be set from XAML.
- AddOwner to the ContextMenu.IsOpenProperty, adding a property changed notification.
- when the IsOpen property changes set the IsOpen property of 4 child context menus.
- using ContextMenuService set the Vertical and Horizontal offsets of the context menus to make them appear in each quadrant; binding the actual height and width properties to calculate the offsets.
[1] Need to derive from context menu otherwise you cannot assign it to the ContextMenu property on the Window.
This appears to work, there are issues with the menus NOT staying open (they're being closed as the focus is outside the menu) but I'm sure using Reflector.NET find a way around this.
This is my first custom WPF control that I've attempted to write; and not sure if this is best approach.
Any suggestions/ideas on how to create this Quad Context menu?
I can think of two approaches, neither one are necessarily that clean but has the potential to work if you have the time.
Approach 1
Use a context menu but through setting the Template make it so that the ContextMenu really just shows a control that happens to open other windows. Perhaps tricks can be done so that opening these secondary menus doesn't take the focus away. This might end up still causing the original problem.
Approach 2
What happens if you set ContextMenu.StaysOpen to false and then try to control when the menu closes yourself?
UPDATE
OK. After playing around with custom controls I have something that is working better than before. All menus now stay open.
What I've done:
- created custom control named QuadContextMenu deriving from ContextMenu, override the default style key.
- the style for the control has 4 popup primitives:
- each with PART_ name so I get a reference to them when the OnApplyTemplate.
- each has child QuadMenu, which is custom control deriving from MenuBase.
- each has their VerticleOffset and HorizontalOffset properties binding to the ActualHeight and ActualWidth of the child [2].
QuadMenu Style/Control Template:
- has a classic border with a dockpanel, and a border with a textblock to put the menu name.
[2] How to position the menus into the Quad:
- TopLeft is offset negative the actual width and height
- TopRight is offset negative the actual height
- BottomLeft is offset negative the actual height
- BottomRight is NOT offset and is in it's original location
NEW QUESTIONS/ISSUES
These are the next challenges to making this control:
1. at the moment keyboard and mouse is locked/captured by the first menu you focus on and the others are not available by hovering over them. which means you can only use 1 of the menus.
2. flip the MenuItem so that for the TopLeft/BottomLeft the submenu opens on the left hand side.
I've already attempted to replace the menu item by overriding the GetContainerForItemOverride methods to return QuadMenuItem. QuadMenuItem is a custom control derive from MenuItem. When I do this however I lose the submenus functionality which is frustrating.
If anyone has any tip/tricks/suggestions on how to tackle these issues it would greatly appreciated! :D Thanks