Datagrid Command Binding to buttom, don't select row - wpf

I have a command bound to a button on my datagrid (e.g. button shows for everyrow).
Basically the command is wired to an object which shows children of that row in a pop up window.
Problem is the customers wants it to display the chid data but not actually select that row in the datagrid.

you have some options:
you can use a style
you can the UnSelectAll() method
and if you google you will find some more
EDIT:
did you try to handle the PrevievMouseDown Event? you can set e.Handled=true if the user click on the button.

Related

How to remove default focus set on combobox in winform

In windows form, I have 5-7 combobox on one 3 tab pages of tab control.
This tab control reside in a group box.
There are other two groupboxes containg other text box and listview controls.
Problem is, When defualt load the form control then focus set on combobox and unable to unselect.
Tried by changing property- causevalidation to false but no output.
Please guide.
Add a line on Page Load to set focus on the control you want to focus.
For Ex:
txtName.Focus();
EDIT
Use this
ActiveControl = yourcontrolNameToWhichYouWantToSetFocus;
You can set the TabIndex of the controls, the lowest index will be the first focused. (TabStop should be true)
I you want to set focus to another combobox you should call Select() method.

Implementation of menu buttons in WPF/MVVM/Prism

I have a Region in the top left of the WPF application which I want to be my global button bar where the user chooses which screen they want to view, and the appropriate content will then be displayed in the main region. THere will also be a sub navigation region in the top right with sub-menu options within that screen, eg if the user clicked "Maintenance" from the main menu, the sub menu would show options for New, Update, Delete, Edit etc.
What I want to have is a way to create a custom menu bar by simply specifying a list of Text button names, and ICommand/Parameter pairs for the action to invoke on button click. The natural way to do this would be have a MenuButtonViewModel class with dependency properties Title, Command and CommandParameter. One more would also be needed, IsSelected, so there is some visual way for the user to see which screen you are currently on, so it needs to behave like a toggle button, but where only one button in the group can be selected at a time. I can then have a UserControl where the content of the bar binds to an ObservableCollection and uses data templates to render the button bar.
I have tried a few ways of doing this so far but cannot figure out a way that gives me the visual behaviour I want. These are my requirements
1) Button to change background to a different Brush OnMouseOver
2) When button is selected, a different Brush is displayed as the background until a new button in the group is selected, like a togglebutton IsSelected behaviour
3) Only one button in the group can be selected at a time
The ways I have tried to do this so far are
1) Extending RadioButton with my own class, adding dependency properties for command and commandparameter. Setting all controls to have the same group Id. Data template to override display of radio button to make it look like a visual button with triggers for mouseover and isselected.
This works fine, except for one thing. Once you select a radio button in a group, there is no way to deselect all options in the radio button group. So if you navigate to "maintenance" and then click the sub menu for "Countries" for example, then you are displayed the country maintenance screen. If you then go to a different area of the app and select "Deal Entry" from the main menu, you are taken to the deal entry screen. If you then click "Maintenance", it displays the generic "maintenance" content and brings back the sub menu control for maintenance, where "Country" is selected in the radio button group, but this is undesirable. When you navigate back to Maintenance, it should deselect all sub menu options, display the generic maintenance landing page content and let you select a sub menu option before displaying that screens content. The first time you load Maintenance, nothing is selected, but once you have chosen an option, then there is no way to have nothing selected again when you reload the maintenance screen.
2) I then tried extending a ListBox, styling it with a horizontal stackpanel for the content, each listboxitem is a menubuttonViewModel. This allows me to only select a single option at a time and to clear the selection when you navigate away from the page. It also lets me change the background when you mouse over each listboxitem.
The bit I can't get working with the listbox is to have the background different on the IsSelected trigger. There seems to be some trigger on the default ListBoxItem template that overrides anything you specify in CSS so no matter what trigger I put on the listboxitem or menubuttonviewmodel style, the background simply does not change. I think I need to redefine the data and content template for listboxitem, but only have it apply for this listbox, so it can't be a global template change to all listboxitems - as I want to be able to use listboxes elsewhere in the app without inheriting this behaviour.
Can anyone give their thoughts on these two approaches and how to perhaps solve the issues I'm having to make one of them work in the way I want, particularly if you can advise how I can override the content/data template for the listboxitems in my style so they do not use the default triggers, and I can get the IsSelected trigger working for me?
If I understood correctly, you would want to clear the selection on the RadioButtons from the Sub-Menu each time you navigate back from another Main Menu option.
In order to solve this, you could have every Radio Button "unchecked" by manually setting the RadioButton´s IsChecked property value to false. A possible solution could be the following:
If you want to clear any checked option from the Sub-Menu everytime you navigate to a different section of the Main Menu, you could first notify to the Sub-Menu´s ViewModel by publishing an event using the EventAggregator after selecting and clicking a different MainMenu button, from the SelectionChangedEventHandler method of the Main Menu´s ViewModel.
Therefore, the Sub-Menu EventHandler of the published event would update each RadioButton´s IsChecked property value to false, which it could be accomplished by using a "Two-Way" Binding between each IsChecked property defined on the View, and each boolean "RadioButton1IsChecked" property implemented on the ViewModel.
I hope this helped you.

How to detect a click on the selected row of a RadGridView control

My WPF application uses the Telerik RadGridView control on a few of its screens. I have a requirement that says that whenever the user clicks on a row in the RadGridView, I'm supposed to switch to another screen and display detail information about that row. I hooked up a handler to the SelectionChanged event and this works, except that nothing happens if the user clicks on the selected row a second time. This makes sense, as the selected row isn't being changed.
How can I detect a second click on the same row and have the second screen displayed?
Tony
You could just attach a handler to the MouseUp event on the GridView. Check if there are any selected cells and respond from there. This will fire even if there is already a selection.
The MouseDown event will fire on the mouse click, but before the gridview updates the selction, mouse up should fire when the selection has already been adjusted
You can also attach a handler to each individual cell in code-behind as follows
(this.GridView as RadGridView).AddHandler(
GridViewCell.MouseUpEvent,
new EventHandler<Telerik.Windows.RadRoutedEventArgs>(this.OnMouseUp));
I think you may try to achieve this through the MouseLeftButtonDown event or PreviewMouseLeftButtonDown event.

Datagrid Row Selection On Button Click In WPF

I am having a Datagrid that gets populated with values from a DataTable. In my program i have four buttons: Goto First, Last, Next and Previous, as the name name indicates i have to select the rows based on the selection made using these buttons. Everything seems well if i use the below code to get the row (for example first row).
DataGridRow row =(DataGridRow)userControl.m_DataGrid.ItemContainerGenerator.ContainerFromIndex(0);
row.IsSelected = true;
But the code throws null value when there is more rows than the height of the Datagrid(When scrollbar comes into picture).
Please help me out of this issue. I think this is because of the view problem.
Due to virtualization the containers are only created when the object is in view, so you could first scroll the item into view using the respective method, wait for the creation of the container and then select it.
As this is rather messy i would suggest binding the IsSelected to a property on your item using a style for DataGridRow (set it as ItemContainerStyle). Then you can just set the property to true and scroll the item into view if need be.

Cannot seem to fire DeleteCommand when button is an datagrid. Selected event always fires

I have a Wpf Form with a tabControl with 2 tabs "Customers" and CustomerDetail"
First Tab is a list of all Customers populated in a datagrid and delete Button as a dataTemplate on each line of the grid.
Selecting a customer it gets the customer details and moves to the second tab.
This all in MVVM and the delete button is mapped to a DeleteCommand .All good.
My Problem
It cannot seem to distinguish when clicking on the delete button it fires the selected event and goes to the other tab.I dont want it to go to the other tab when pressing delete.
How can I do it?
User selects row and should go to the other tab.
Row is selected and presses delete button just trigger the delete command.
Hope all clear
Any suggestions?
Try using DoubleClick event for showing CustomerDetails tab. so, when user click on delete button, they just fire click command of the datagrid and nothing happen...

Resources