I want the controls in my wpf window to change depending on whether the user is viewing or editing/inserting data. For example, I want to display a label at view time, but a text box (or combo box etc) at edit/insert time.
Can I do this with DataTemplates or do I have to have two controls for each data item and change visibility depending on what mode the form is in?
If DataTemplates will do the job, can anybody point me to some examples?
many thanks
mcalex
In my opinion, the better way to do this is using differrent data templates for control, in case when you always need to use them together.
In any case you have to select switch templates or controls mechanism. You can use DataTrigger (first answer) or ValueConverter
Related
I am trying to achieve this look&feel in WPF.
It basically acts like a TabControl, with the tabs on the left (vertically)
The right side of the window completely changes depending on which item you have clicked on the left "nav bar":
Oculus UI
What I did:
I started doing a Custom Control. A grid with two columns, a StackPanel in the left column in which there will be a clickable button/label for every menu entry (vertically of course). A Property MenuEntries which is a List<string>.
How do I get the control to add a "tabpage" (a grid/canvas?) programmatically for every MenuEntry the user adds at design time?
How can I achieve the behavior of the TabControl during design time, i.e. the content on the right side changes as soon as the user clicks an item on the left (in the designer/editor)? (kind of like the TabControl itself?)
Is this the right approach or would you do that completely differently?
Yes, you can use TabControl for that, just set TabStripPlacement="Left", (some more details)
The whole rest: colors, margins etc is set via styles.
Also, to save you a lot of trouble, use MVVM (with some framework for WPF) for that. Set ViewModel collection as ItemsSource for the TabControl.
Then, when you select one of the tabs, the control will display the view for the selected VM.
DO NOT set TabItems in xaml for each tab, this is wrong way to go in the long run.
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.
I have an ItemsControl.I want to hide the Text Box in the ItemsControl DataTemplete on some Flag. But when i access the Text Box in Code Behind File .it gives the error doesn't exists in the current context.Is there a way to do this using SilverLight?
Let's go by parts, in case you are not using MVVM, due to you are using a DataTemplate the Control is not added as usual in the visual tree, so you can do the following, add to the TextBox the LoadedEvent, and create a List in order to have all the TextBoxes inside, and there you can do what you need when you want.
In case you are using Binding with MVVM, you can bind the property of that control, like the Text with Mode=TwoWay or the Visibility with a bool and adding a converter.
That is a general answer to your general question, in case you need more details, please add the specific code you are using.
Is there a way to make long items wrap in a .net windows forms application, I have already got LabelWrap set to true, but it doesn't work, I am using Details View Type,
Thanks
ListView doesn't allow text-wrapping.
What I usually do is set "ShowItemToolTips" property true in such cases. Then user can see the whole text a ToolTip. This is the only simple option I can see with ListView to help in this case.
If you really want to support for text-wrapping you will have to either use a third party ListView or use a DataGridView.
EDIT
If you are using VS2010, Tile View is an option for you.
With the tile view feature of the ListView control, you can provide a
visual balance between graphical and textual information. The textual
information displayed for an item in tile view is the same as the
column information defined for details view. Tile view works in
combination with either the grouping or insertion mark features in the
ListView control.
The LabelWrap property only works for icons (View types of LargeIcon or SmallIcon), not Details. As others have already answered, you have to use DataGridView or a 3rd party listview such as ObjectListView.
Here's a screenshot of my application:
Basically, depending on what option is selected I'd like to show another 'content' which can be a buttons, or forms or whatever.
What would be the best choice for this? Using MDI? I'm really new to this type of thing.
This scenario lends itself well to tab pages, as you'd find on a TabControl.
However, since you already have a mechanism for switching between the content, you might prefer to create a series of Panels whose Dock property is set to DockStyle.Fill. When the user clicks the appropriate heading, you simply need to show the appropriate panel and call BringToFront() on it. This is essentially what the tab control does internally, anyway.
Don't forget to use SuspendLayout() and ResumeLayout() appropriately to reduce flicker, which can be a huge problem in WinForms applications, especially when there are lots of controls.
You can position a TabControl where the buttons are not visible and control it from your buttons.