I put the ClearButtonVisibility in an entry and the entry inside a fame and the Clear Button Visibility doesn't work, how do i make it work
int the xaml code i have this:
<Frame Margin="35,0,35,0" Padding="0">
<Entry Placeholder="This is a freaky entry"
ClearButtonVisibility="WhileEditing"/>
</Frame>
The ClearButtonVisibility property can be used to control whether an Entry displays a clear button. You can set it to WhileEditing, that indicates a clear button will be displayed in the Entry, while it has focus and text.
However, several Github issues for ClearButtonVisibility could be found.
Entry ClearButton does not clear the text? #9954
ClearButtonVisibility is not working #6360
You could follow this thread. Referring to the above thread, i think you could update your Visual Studio. Or remove Padding in VerticalStackLayout as ismasanchez said.
For more info, you could refer to .NET MAUI Entry.
Related
Using WPF InputBindings and KeyBinding, I am looking to find if there is a way to get when a keyboard input is being held down, and next when its being released (like the standard Preview/KeyUp hooks).
For example, if user was Holding CTRL+Right, I don't want a flood of messages into my ICommand, but instead a single execution is wanted. When the CTRL+Right is released (KeyUp), it would be nice to handle that with another KeyBinding.
I had considered inheriting from KeyBinding or InputBinding, but there are no events I can find to hook into so I can ignore the IsRepeat of a a possible keyboard event.
I am not looking to do this is code-behind, this is entirely MVVM; Goal is to do this strictly with InputBindings. This will be part of multiple controls, so preventing code-behind keeps the code clean without copy/paste.
<UserControl.InputBindings>
<KeyBinding Gesture="CTRL+Right"
Command="{Binding CommandHotKeyExecuted}"
CommandParameter="{x:Static SomeConstantValue}" />
</UserControl.InputBindings>
Would be sweet if this was available via the Gesture, but can't find anything other than simple statements like CTRL+Right;
Reference:
Source for InputBinding
Source for KeyBinding
MSDN KeyBinding
Alternate ideas that are not ideal -
Doesn't handle the KeyUp; Has CodeBehind requirements:
SO: Ignore repetition in KeyBinding (holding the key - execute command just once)
Not talking about holding keys down: SO: Keybinding in WPF
Backstory aka My Configuration
Like many, I have an application which has a Menu docked to the top. I wanted this menu to be hidden until I pressed the alt key, where I'd then be able to navigate this menu either using my keyboard or by clicking my way through the MenuItems.
In my ViewModel, I made a boolean property called ShowMenu, an ICommand called ShowMenuCommand which operates as a flip flop for ShowMenu, and proceeded to wire things up with data binding. You can see that here:
ShowMenuCommand = new RelayCommand(
_ => ShowMenu = !ShowMenu);
The Menu is set up as such:
<Menu Name="MainMenu" DockPanel.Dock="Top"
Visibility="{Binding ShowMenu, Converter={StaticResource BoolToVis}}"
LostFocus="MainMenu_OnLostFocus">
I also configured keybindings for left and right alt that fire the ShowMenuCommand.
<Window.InputBindings>
<KeyBinding Key="F1" Command="{Binding AboutCommand}" />
<KeyBinding Key="LeftAlt" Modifiers="Alt" Command="{Binding ShowMenuCommand}" />
<KeyBinding Key="RightAlt" Modifiers="Alt" Command="{Binding ShowMenuCommand}" />
</Window.InputBindings>
This works exactly as I configured it to work: The menu is normally hidden, but when I press alt it appears and allows me to navigate the menu items until I click away or I select an item and it loses focus, the LostFocus handler setting ShowMenu to false.
The Problem
Having done this, I seem to have lost the ability to enter Keyboard Navigation Mode. For those who don't know what I mean, normally when you press alt in a gui app, certain characters become underlined, and you can press those characters on your keyboard to navigate the UI. I don't know the formal name for this, so bonus points to anyone who can provide the actual name.
Sources for getting this far
How to make WPF menu bar visible when ALT-key is pressed
LeftAlt Keybinding in WPF
What Now?
I've been searching high and low, crawling through keyboard classes and UIElement in hopes of finding something to fix this, and I've come up with a couple possible solutions. That said, the reason I have resorted to StackOverflow is because I don't know how to pose my google search in such a way that I find what I'm looking for.
My proposed solutions are as follows:
Modify ShowMenuCommand to toggle keyboard navigation mode in addition to modifying visibility.
Remove my keybindings and wire the menu's visibility to whether keyboard navigation mode is enabled.
I thought I would've had this with UIElement#IsInputMethodEnabled, however this does not appear to be the case. That said, I don't know if it matters what element you select, and I don't remember if I tried targeting the Menu or the Window.
If anyone else has any third idea or might know something I'm missing, please do let me know. Hiding the menu bar until you unhide it with alt should be something terribly trivial to set up, so I'd not be surprised if I'm missing something.
Additional Context
If you would like to see any code surrounding the provided snippets, you can view the project source code on GitHub.
So after some additional research and a some help from #Vlad, the final solution is the following:
Menu visibility is bound to ShowMenu property in View Controller.
Window has KeyUp event handler which watches for the alt key.
private void MainWindow_OnKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt))
{
MainWindowViewModel mwvm = (MainWindowViewModel)DataContext;
mwvm.ShowMenu = !mwvm.ShowMenu;
}
}
This works, except the menu will remain visible after an item is selected. I tried creating an item template to take care of it all at once, but it didn't want to fire.
Handle Click event on relevant menu items, skipping those that are just categories.
The only thing this doesn't take care of is when the user clicks away from the menu. I tried attaching a LostFocus event to the Menu, however this event fires when the menu opens, perhaps because the focus is being taken away from the Menu itself and onto the ContextMenu provider or whatever actually handles drawing the open menu.
I'll need to do some more research and some code cleanup, but the above solution works relatively okay.
Edit: I'll leave this answer unselected for a couple days just in case anyone else has any other ideas.
Edit 2: I found a solution for my use. Since I only have one top-level menu item on this menu, I hooked the MenuItem's SubmenuClosed event. When this happens (either because of a user selecting an option or them clicking away), it hides the menu by setting ShowMenu to false.
It's probably not the most elegant solution, but it's functional. I'll look into cleaning it up later.
I am new in WPF and want to create WPF application like cookbook. I already done this and app work correctly. But I make it in this way:
First screen show buttons, which open new windows to do something. As a result i have 14 different windows. It is ok, but now i want to make it in other way.
I am trying to make one window, which will be showed at start, and change content. I divided window on two grids. First is static and is placed on bottom. It contains buttons, which represents functionality of the program. Second one will be dynamic. There i want to show content of every window. So i want to change content of this panel instead of creating new windows.
I tried to make *.cs files which will create controls in code-behind, functions and data. But my idea is not succesful and i do not know how to do this.
At all, I want to create app, which will work like this:
- if you click button "Add receip" then app will show controls to add name, ingredients and save it at the end.
- if you clik "Show receip" previous content will be replaced by list of ingredients
and etc.
I hope you will understand me.
You can create a Frame instead of second grid. Frame allows you to show pages, and not in seperate windows, in Frame itself. You can navigate the frame into the page like
mainFrame.Source = new Uri("Page1.xaml",UriKind.Relative);
This changes the frame to your page. You can change the source again, if you wanna change the page again.
Note: You can add tags to your buttons like "showReceip" and you can make just one buttonclick event for your buttons. Code will look like this.
mainFrame.Source = new Uri((sender as Button).Tag.ToString() + ".xaml",UriKind.Relative);
That takes the tag of your clicked button, add the string ".xaml" on it and take it on the source part. So, if your tag is "Page1", Source will look like "Page1.xaml" as my solution.
Appreciate the try, I hope you are looking for WPF user controls instead for separate windows. User controls are similar to windows you can create the UI and functionalities in the user control. I would like to recommend you to design the main window like the following:
<Grid>
<Canvas Name="canFunctionalButtons">
<!--Define the buttons inside this canvas
And allocate proper place for this in the UI
-->
</Canvas>
<Canvas Name="canControlContainer">
<!--This is to display the user control
Which can be changed dynamically according to the Button's click
-->
</Canvas>
</Grid>
Then you have to add click event for those buttons, which will add specific user control to the canControlContainer canvas. An example for adding an user control to this canvas is as follows, Let btnAddSomething be a button and btnAddSomething_Click be its click event then you can do something like:
private void btnAddSomething_Click(object sender, RoutedEventArgs e)
{
canControlContainer.Children.Clear(); // will remove previous contols from this canvas
// UC_AddSomething be the user control that you wanted to add here
canControlContainer.Children.Add(new UC_AddSomething());
}
Yesterday I was refactoring some code in my windows phone project to try and use mvvm. I added binding to the toggleswitched on the page etc. the previous code also had evenhandlers for each toggleswitches checked and unchecked events. anyway I managed to clean it up. But my problem occured when I was trying to get code in my viewmodel to execute when i navigated away from this page. Initially I tried this
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
viewmodel.SaveSettings();
}
after a bit of time debugging. I found this method wasnt being called. This was due to the fact that I was calling it in the code behind of a UserControl. Ive also tried to call the OnLostfocus() method when navigating away from the User control. but this doesnt work either. for the most part the project swaps in and out usercontrol elements in the main xaml.cs. mainly iam not really sure how to go about getting this method to be called when the usercontrol exits without destroying the mvvm structure i have i place now. any help would be greatly appreciated.
Solution:
oKay i figured this out. In my main.xaml is had a user contorls beign swapped in and out depending on menu item press by the user. the usercontorl in the main xaml looked like this
<UserControl x:Name="ActiveUserControl" Height="Auto" Width="480" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1" Margin="0"/>
and it was swapped in and out using this command in the xaml.cs
ActiveUserControl.Content = _userControls["controlname"];
_usercontrols was just a dictionary of usercontrols. so in the onnavigatedfrom method in the main.xaml i changed the content of the ActiveuserControl to some other usercontrol. so if the home key was pressed on this usercontrol it would firethe unloaded event which fired then in the usercontol code behind this way it saved my settings.
I have SEVERAL XamDataGrids in my application, and I want all of them to enable the respective Save buttons as soon as the user changes a checkbox in them. This currently doesn't happen until I leave the cell or press enter etc, because the cell is still in edit mode. I know how to fix this using a post I found in code-behind:
private void XamDataGrid_CellChanged(object sender, Infragistics.Windows.DataPresenter.Events.CellChangedEventArgs e)
{
e.Cell.Record.SetCellValue(e.Cell.Field, e.Editor.Value, true);
}
But how can i handle this for ALL my grid across the app, without putting this in code-behind for every grid? I am using MVVM and would prefer to not have any code behind, if possible. If I have to, i will, but i defintitely don't want it in the code behind in 17 different files with grids. Maybe a behavior?
You need to set the DataItemUpdateTrigger to OnCellValueChange.
Default value for all fields in a XamDataGrid
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings
DataItemUpdateTrigger="OnCellValueChange" />
</igDP:XamDataGrid.FieldSettings>
I believe this could easily be applied as a style for all XamDataGrids.
For an individual Field
<igDP:Field Label="" Name="IsSelected" >
<igDP:Field.Settings>
<igDP:FieldSettings DataItemUpdateTrigger="OnCellValueChange" />
</igDP:Field.Settings>
</igDP:Field>
DataItemUpdateTrigger property is available in FieldSettings class in NetAdvantage 2013 v. 2 or later.