Ugly WPF hotkey - c#

I have some commands that use Ctrl-Win- as hotkeys. However, MenuItem displays these as Ctrl-Windows-, which is quite ugly. How can I modify it so that "Win" is displayed instead of "Windows". Yuck. Help!

Set the InputGestureText property of the MenuItem. Just remember to do it before the command binding.
EDIT
If you do not want to repeat that for each Menuitem you may iterate all the MenuItems to change their InputGestureText property programmatically. As alternative, if you're using custom input bindings, you may derive a class from KeyGesture and override GetDisplayStringForCulture to do the replacement.

Related

Silverlight-Hiding item in ItemsControl

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.

Display TextBox Or TextBlock depending on value of a Flag

In my WPF application, my Viewmodel has a boolean property IsOwnerOf and a string property Title. If IsOwner==false, I want a TextBlock displaying the Title (because if you're not the owner, you should not be able to edit it) and if IsOwner==true, I want a TextBox displaying Title - obviously at the same place in the view.
Also I don't want to do it codebehind since I follow the MVVM pattern. Thought about Style.Triggers, but with them I can only influence attributes of an element, not the element type itself, or can I?
EDIT:
Practically the answers below regarding triggering Visibility or IsReadOnly work, but I still would like to see a conceptually better answer! What if I replace the TextBox resp. TextBlock by elements that don't have these convenient properties? There must be a better way than creating both and hiding one of them, that just doesn't sound right...
The easiest option is to always drop a TextBox and bind it's IsEnabled or IsReadOnly property to the IsOwner flag.
You can also use a DataTemplateSelector to achieve this.
You can use triggers to change the Visibility of your TextBlock and TextBox using a BooleanToVisibilityConverter

Silverlight ChildWindow with DataTemplate

I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)

wpf: how to edit selected item with possible cancellation?

I basically want to create something like this:
So, a user can add/remove items from the list and edit them in the red panel below the list. When the item is selected, the changes can be made in the panel.
Then, the changes can be either saved or canceled with one of the buttons below. User cannot select another item in the list without explicitly cancelling the changes.
How to do this? I only see the option of making the red panel a separate control and changing its DataContext manually on ListView.SelectedItem changes. The red panel's DataContext is a special wrapper on a ListView's item which has 'save' and 'cancel' options. ListView is set to IsEnabled = False so that its SelectedItem doesn't change when editing is in progress.
How would you do this?
The question is quite close from, for example, this one :
How do I stop binding properties from updating?
Anyway the WPF Object that will handle this is the BindingGroup :
http://msdn.microsoft.com/en-us/library/system.windows.data.bindinggroup.aspx
the most easy apporach is to use a dialog for edit a selecteditem. thats what i do in my project. i use this dialogservice and handle the result.
if you want to handle all in one view you could set a property SelectionEnabled=false when the SelecteItem is set. and then SelectionEnabled=true when the save or cancel command is invoked.
the datacontext for your edit panel is simply your SelectedItem.
You can try to use bindings with UpdateSourceTrigger=Explicit. The blog post Edit With Explicit UpdateSourceTrigger will give you more information about how this can be implemented.

Changing the standard behavior of a TextBox?

I have an application that declares textboxes in various places, like in styles and datatemplates, and now I'm in a situation where I need to change every textbox's standard behavior for getting and losing focus.
What's a good way to do this?
I was thinking of two solutions: one is to derive a new class from TextBox, which I understand is generally frowned upon. The other is to create some kind of style that uses EventSetters, but since the styles and datatemplates in my application don't have codebehind files I donno how an event will find the appropriate event handler.
You can create a style that applies to all TextBoxes using the Key property as follows:
<Style x:Key={x:Type TextBox}>
...
</Style>
You can then modify the Template property of the TextBox and use Triggers to add special behavior to the OnGotFocus and OnLostFocus events.
Based on your feedback, I'd recommend an attached behavior used as follows:
<TextBox b:TextBox.SuppressOnFocus="True"/>
The attached behavior implementation would simply attach to GotFocus and LostFocus and clear/reapply the binding as appropriate.
Under normal circumstances, I, too, would frown upon subclassing TextBox. In this case, since you are changing the behavior of the TextBox, a subclass may be your best option.
If you are going to use this functionality in only one project then you can create UserControls which has a TextBox and access the the OnFocus properties. You can also make a Custom WPF Control which derives from a TextBox and then implement the LocusFocus event.
I have used the same approach to create a User Control TextBox which performs validation:
http://www.highoncoding.com/Articles/578_Creating_WPF_TextBox_UserControl_to_Perform_Custom_Validation.aspx

Categories

Resources