I have a relatively simple case where I want to repurpose a "Start" button with an "Abort" button after it is first clicked.
I'm wondering what is the best way to implement this button repurposing using XAML, hopefully declaratively rather than with a code-behind hack:
<DockPanel>
<Button x:Name="btnStart">Start</Button>
</DockPanel>
I tried the following with using two distinct Buttons and toggling the Visibility property of each but it does not reflow the layout within the DockPanel. I want the buttons to share the same layout space but mutually exclusively. This also has the problem where only the last element fills the dock panel and the first element is squished off to the left.
<DockPanel>
<Button x:Name="btnStart">Start</Button>
<Button x:Name="btnAbort" Visibility="Hidden">Abort</Button>
</DockPanel>
I prefer the two Button approach so that I can have separate Click event handlers. Please don't suggest naive solutions like dynamically adding/removing Button elements. I'd prefer a declarative approach if at all possible. Thanks!
Did you try to set the Visibility property to Collapsed instead of Hidden? If you do that, the unused button should not use any layout space.
If it's a viable option for you, you could try downloading the WPF toolkit and creating a single control with multiple states using the VisualStateManager.
Everything can be done declaratively in the XAML and switching between the States is fairly simple...especially if you're familiar at all with Silverlight development.
I don't mean to be snarky, but I wouldn't do it, unless you also change the visual state and look of the button to feel like something that would have that behavior, like an animated toggle switch or something. If the button launches a process which is slow running and you want to be able to abort it, I would think that (a) having a separate progress dialog showing the status and a cancellation option or (b) enabling that status and cancellation control in a different part of your window would be more consistent with other user interfaces. Also, accidental double clicks will immediately cancel the action, which might be odd.
When the button is clicked assign a different command to the button and change the caption. Then when it is clicked it will goto a completely different handler in the code behind.
Related
I am silverlight beginner and i am in a situation that i have to pop up a message box containing three buttons (Yes/No/Ok). Please do not give me the following suggestion(I already tried and tired) :
(1) Use Childwindow : Though it is a good solution but the way it pop ups is not like MessageBox pop up because in message box pop up we can see clearly
the data behind but we cannot edit/click/change it whereas in child window pop up it is less visible. (So if you know any way to make childwindow back ground
visible just like MessageBox then you are most welcome to tell me alternatives).
(2) Use PopUp control : Because it do not pop up exactly and can be distuinguished easily from MessageBox and the data behind is always editable when PopUp control is pop-Uped and if i set it to Collapsed then it show nothing. So i want pop up exactly like MessageBox.Show() Pop Ups.
Whereas in MeassageBox.Show() we can see that when it is pop uped the GUi behind is visible but not clickable and the way it Pop ups is perfect so i want
something that could Pop up exactly the same way with three buttons on it.
Could some one please help me in achieving my target ?
If I understand the question, you want a popup in the default MessageBox style, only with Yes/No/OK buttons. Recognize that there is not an ideal solution -- even the Windows API (which you don't have access to in Silverlight outside of full trust) doesn't provide this combination of buttons (without using advanced techniques anyway).
So, you are left with the non-ideal options of making use of Popup or ChildWindow. I think it would be misguided to try and mimic the exact style of the Windows dialog popup, since that style depends on the system, which you don't have access to from Silverlight. Instead, I recommend just choosing a style that you are comfortable with, and which matches the theme of your application.
In any case, as with all controls, you have two options for styling ChildWindow:
1) Modify its style properties like OverlayOpacity, BorderBrush, etc. Take a look at the default control template to see how those properties are used in the control's presentation.
2) If you need to do more customization than the default template allows, then you can always copy-and-modify the ControlTemplate. That allows you fine-grained control over everything about the popup's appearance, including its open/close animations. There's lots of info on the web about modifying control templates (eg, MSDN).
I'd like to control which button is focused in my view. User must be able to perform a job without need to use mouse. And job is going through all elements. Depending on element, some buttons will appears, some disappears.
I could do it with dependency properties. To example, if there are buttons Previous and Next, then I can provide IsPreviousFocused and IsNextFocused to set focus to them after certain event. Events can be: showing window for the first time (something should have focus already), reaching 1 element (hiding Previous button, setting IsNextFocused), reaching last element (opposite), etc.
This looks reasonable more or less.
But, if I have, to example, 10 buttons (different operations), then all of them will have to have dependency property!
So I was thinking about much easier approach: when I hide button, there will be no focus
if(FocusManager.FocusedElement == null) { ... }
If I can detect, when there are no focus, then I can try to set it to one of the buttons somehow. This way, I don't really need any complicated focus management
Question is: how to deal with FocusManager.FocusedElement in MVVM scenario? How to detect when there is no focus (when window is shown first time, when certain button is clicked and become invisible, etc)?
P.S.: I actually hate ms for making another technology without thinking fully into it; focus is very basic feature (and everybody care about it in their software), but there is no direct support for it (in xaml); looks like "oh, its too complicated, lets skip it" solution.
You could control your focus from your ViewModel by using the approach shown here:
Set focus on textbox in WPF from view model (C#)
This one is a bit tricky to explain.
I have a usercontrol with some textboxes. I also have a menu just above this usercontrol in the same window. Whenever I tab away, the LostFocus fires correctly on the textbox, and this is what I want. Strangely enough, if I click the Menu button on top of my window, the LostFocus event does not fire on the textbox. Is there an elegant way to make sure that my menu properly allows LostFocus to fire on any controls which last had focus?
I also want to avoid having to Update BindingExpressions otherwise I would likely be doing this for N textboxes, which is undesirable.
I can't imagine it is too difficult to achieve this.. I just don't understand how this doesn't work: in most other situations LostFocus always fires.
Any ideas? Thank you.
Is the menu WPF as well or Winforms / UnManaged? If either of the two then the lost focus event does not fire. This can play havoc with WPF controls as many time a save or other data function is being performed from the menu. To counter this I have had to implement multiple ways to combat this. The easiest way was to implement a mouse leave event on the user control itself and perform any actions you require manually in code.
I want to make a complex layout, and would like advice on the best way to go about this.
Here is the behaviour I'm looking for: Layout of some text blocks and some images, such that a tap anywhere on the layout will go to another related page to that item. I want a long list of this item in a scroll viewer.
First, I tried to make a grid, add the items in it, then add this grid in the outer grid, the main one for pressing, I made an event handler for mouse click, but a problem appeared; that when I try to scroll "on the item to view the all list" the event handler fired!, I tried the mouseButton up and down, and the same happen
Second, I'm not sure if it's an efficient way to make it.
I want a good way to design this complex layout, and of course it will be a programmatic way, and a way to recognize the item I press on.
It sounds like you're trying to create your own ListBox control. Using this will give you the scrolling functionality for free, and you can use the ListBox's SelectionChanged event to determine when an item has been clicked.
The best way to design the layout will depend on how complex it actually is. If it merely contains an image and some text, you should have no problem hand-coding that in XAML. If there's a lot more to it than that, I'd recommend looking into Microsoft's Blend tool. Whichever route you choose to produce the XAML, you will place the code in the ListBox's ItemTemplate, which determines how each item in the ListBox is displayed.
A good walkthrough of something similar to what you're doing can be found at http://weblogs.asp.net/psheriff/archive/2010/10/27/windows-phone-list-box-with-images.aspx
I'm suspecting the answer is no, but I just want confirmation before I move on - is there any easy way (as in, not re-templating) to hide things like the box that gets checked on a CheckBox, the drop-down arrow of a combo box, etc. without hiding the actual content being displayed?
Try this! Example is for a radio button:
radioButton.Visibility = Visibility.Hidden;
You can play some around with the Background or BorderBrush, but the answer is as you excepted: No..
You will need to restyle the control, but don't let the whole styling and templating scare you though.. Its actually quite easy once you get the hang of it ;)
Notice that you can have a checkbox like button using "ToggleButton", so you could use more or less use the same template as for your normal buttons (if you need them for toggling states).
The answer is YES, as you can actually template all controls a lot, without the need of creading custom controls. Just set the template in a style and add the style to the control.
oh i just reread your question
.. the answer is NO, but have a look at the toggle button maybe its what you are looking for.
the simplest way to display the content of ComboBox, CheckBox, etc. is to display a TextBlock in readonly mode.
You can easily make an UserControl with a DependencyProperty and show/hide your content.
You can disable the control/s in which case they cannot be interacted with but the text is still displayed.