How do I get WPF control to mimic the disabled state? - c#

Basically I have a custom WPF button that I would like to look disabled but still be able to react to the button being pressed.
I'd prefer not to tie into other event types if possible, because there's some funny timing issues with touch and hold going on already.
Thanks!
This is what I wound up doing as a workaround:
this.myLabel.Foreground = Brushes.DarkGray;
this.mySeparator.BorderBrush = Brushes.DarkGray;
this.myOtherLabel.Foreground = Brushes.DarkGray;
I'm not a huge fan because it's hugely custom to the components of my custom control, but this is at least a quick programmatic way to do it until somebody has something better.

You can get the current template of a control like this:
System.Windows.Markup.XamlWriter.Save(myButton.Template, new FileStream("c:\\bla.xml", FileMode.Append));
Examine the template triggers to see what changes if the button is disabled, and set the properties accordingly.
Edit: OK, that was not it... :-)
You could also set the button disabled, and then use Snoop or Show me the templates to inspect the values of the layout properties -- or, programmatically, save them to a Style -- then enable the button again and apply the style to it?

Related

In a property grid is there a way to unselect all grid elements programatically?

I am working on a project in which I am using a property grid to display the properties of the selected control.
The Property Grid is fixed to the left edge of the container and in the rest of the space I have the form I am designing.
On clicking a control on the form, the specific control’s property is getting selected.
In the above figure, I have selected the textbox and the textbox’s properties get shown on the propertygrid.
Here if you observe, by default, the Name property is highlighted as well.
Is there some way to unselect this property programmatically?
I have tried some suggestions online but none have helped. I am not able to find find a way to remove all selections from the PropertyGrid, but its behaviour seem to be different form a DataGrid...
Here is why I need this...
On selecting a control, if a property in the property grid is selected, then the property is getting modified.
For example, If i cut the control using Ctrl + X, the selected value in property grid is getting cut which in some cases is forcing user to set the property before modifying anything on the form.
I have tried selecting multiple controls, but in that case alse the selected property seems to be persistent
Since PropertyGrid uses DefaultProperty to select a property in its grid, as an option you can set DefaultProperty attribute at run-time for your object to a non-browsable property, for example:
this.propertyGrid1.SelectedObject = null;
TypeDescriptor.AddAttributes(someControl,
new Attribute[] { new DefaultPropertyAttribute("Site") });
this.propertyGrid1.SelectedObject = someControl;
Well, what you are trying are hacks. It is never a good idea to do such hacks particularly if you are not the only person that use the software.
In your case, the focus should be on the designer while you interact with it. So if the user press Ctrl+X, the designer should respond to the keyboard and it should not have any effect on the property grid (as only one control can have the focus at the same time).
Thus it is up to you to make sure that your designer is focusable, that it has the focus when initially displayed, that it get the focus when you press the TAB key. Pressing the TAB key again should put the focus on the property grid so that user can interact with the grid without using the keyboard.
If you have more than these 2 controls, then obviously TAB should also stop at any appropriate controls. Also, it can be a good idea to have some direct shortcuts like F4 to (show and) activate the properties pane.
If you are not able to make it works, then the best compromise would be to use another windows for the properties grid. By using a distinct Tool windows for the properties, it should not respond to the keyboard when the main windows has the focus.
Here are some links that might help you:
Panel not getting focus
Control.Focus Method() — See Remarks section.
In any case, you should not prevent Ctrl+X to works as expected when the property grid has the focus and a property is selected. Users don't like software that do not follows UI conventions.
As a software developer, you should as much as possible ensure that your application follows standard behaviors. I recommend you that you take one or 2 extra days developing your software properly instead of doing hacks.
Often, compromise to gain a few days will never be fix and will be a pain for many years. Better to do it right from the start. Unselecting an item in the property grid is not an acceptable workaround. Your manager should not allows you to do that.

Tab control - wait until control become enabled

I have multiple controls on one form,and when i select some value from combo box(for example 1) next control became enabled, else next control stay disabled.
Problem is that if i just press 1 and tab, after that next control became enabled, but program jump over it just like control are still disabled, and tab control selecting next control.
I need to find way how to tab check is control become enabled and go on this control,and if control are still disabled that go on next enabled control.
Thanx
You created a mousetrap for the user, very hard to escape from. Technically you can handle the keyboard navigation by trapping the Tab key before it can be used to navigate but the user still has an unsolvable problem when he wants to use the mouse to change the focus. He has nothing decent to click on.
You'll need to re-think your UI design. One possible solution is to change the ComboBox's DropDownStyle to DropDownList. Which ought to be pretty appropriate if you use its selected item to enable other controls, there should only be a limited set of valid selections. If that's not what you want then you need to do something drastic. Not necessarily limited to hiding controls instead of disabling them.
This is probably caused by the event of the combo-box you using to control your flow.
The "Changed"/"Value changed" events in most languages fire up after the control has lost focus.
You forgot to add a tag for the UI technology you are using.
If you are using WinForms, then you can try to execute the SelectNextControl method on your control that the user just edited. This will find the 'next' control for you, and activate it.
Lets assume it's winforms (playing with disabled/enabled like this in wpf is.. against mvvm rules).
Firstly, ensure what tab order/index of your controls is ok. To test, if they are all enabled, then pressing Tab should go through them in the right order. This can be seen easily
Next thing is to choose one of many possible solutions, to make 1,Tab to work:
disable Tab key navigation at all;
make controls to pass control (focus) to specific control (making tab order irrelevant);
use SelectNextControl (work best for custom controls, which when will support that tab-flow schema);
prevent focus changing, do all logic, change focus (theoretically).

Change TextColor of disabled control

How to change the color of text, when a control is disabled.I want to set different color when control is disabled in c# winforms.
Edit: I had made the same mistake as Cody in the comments so corrected my answer.
It depends on which control it is.
For example, if it's a TextBox maybe you could make it ReadOnly instead of disabled. And for some other controls you might be able to do similar things to make them appear disabled without actually being disabled.
However, if you want to do it properly you need to make them Owner-draw or override the OnPaint event and draw the text yourself.
You can just do it manually -- when you disable the control, just change the text colour too?
If you have many controls, you can do this:
attach your form OnChildAdded event
in the event, use if ... is of type structure to determine control type
depending on the control type, register proper OnEnabledChange event
in the event, change text color appropriately
That way, you will have a piece of code that will work for all your forms and will gradually expand to use all the controls you need.
I'll provide some code if that's the way you want to go...

ListBox with pop-out/popup item details

I would like to create a listbox, with a details pop-up/tooltip kind of window.
Scenario is as following:
List of items
Show details of selected item
Details should be displayed outside the listbox and overlaying any controls that happens to be nearby.
The problem about using tooltips is that they disappear after a while. And the problem about using pop-ups is that they do not move, when the window moves (?)
So I'm just looking for some pointers on how to solve this.
Use ToolTip object. It has autopositioning and nice graphical style out of the box.
Simply use it like this:
toolTip.PlacementTarget = yourSelectedItem;
toolTip.Placement = PlacementMode.Right;
toolTip.Content = {place whatever you need to display here};
You can control its visibility with the IsOpen property.
Adorners were built for things like these.
That said, if I were doing this I would set "StaysOpen" on a Popup to false. So when the user clicks somewhere else it will automatically disappear (ie when window is moved). Do you really see your users moving the window so often while looking at the details? Going down the adorners route is not all that easy. It has its own complications.

Silverlight: How to make my custom control act like a button

I have a custom control and I would like it to act like a button i.e. when you hover over it changes a little so it seems "clickable" to the user
I actually acheived this using the MouseEnter and MouseLeave events and changing the gradient but...
is there a way to apply a style to the user control and say something like TargetType="button" so that it "acts" like a button automatically?
I feel the way i'm doing it is not the best way
As sniper says, you can set a Controltemplate for each state.
Alternatively, you can completely replace a control's visual tree with anything you want - while still keeping the control behavior intact. Check out this post by ScottGu on the topic
In Expression Blend 3, you can edit the different states (Normal, Hover, pressed, selected etc), of any control how you need it. just select your control and click Edit copy template
Add border object and sets its visibility on mousehover and leave event on the control (This will look like a flat\popup button). Additionally set the control's cursor to hand.
You can derive your control from ButtonBase, just like Button, HyperlinkButton, Checkbox, etc.

Categories

Resources