Which wpf control is best suited for this menu scenario? - wpf

I need a button-style menu, i.e. horizontally arranged group of always visible buttons.
Like radiobuttons they should have a selected property, i.e. the click-command should fire only when the selected status changes to true, not on every click as with a normal button.

Dude, this is WPF, you can use any control that fits some or any of your requirements and then simply provide a new ControlTemplate for it. Incidentally, there is no Selected or IsSelected property on a RadioButton... perhaps you were referring to the IsChecked property? This property is inherited from the ToggleButton, so that may be more appropriate.
As the ToggleButton is already a Button, you could even get away without providing a new ControlTemplate for it.
As for your requirement regarding the Click event, I don't think that you will find that functionality on any of the WPF controls, but it could be manually implemented:
private void Button_Click(object sender, RoutedEventArgs e)
{
if (ToggleButton.IsChecked == true)
{
// Do something here when the `Button.IsChecked` == true
}
}

RadioButton also like other Buttons fire Click event on every click. Also there is no Selected property on the RadioButton.
But if you want your MenuItems to be like Button, then you can use ToggleButton here.
ToggleButton has IsChecked Property which tracks the checked state of button and Checked event which is fired when ToggleButton is Checked.
Also, if you want to automatically check/uncheck your ToggleButtons on click of other ToggleButton then you can use RadioButton as DataTemplate of your MenuItem and override its Template like below:
<RadioButton Content="MyRadio" Click="RadioButton_Click">
<RadioButton.Template>
<ControlTemplate TargetType="RadioButton">
<ToggleButton Checked="ToggleButton_Checked" IsChecked="{Binding IsChecked, RelativeSource={RelativeSource TemplatedParent}}" Content="{TemplateBinding Content}"/>
</ControlTemplate>
</RadioButton.Template>
</RadioButton>

It turned out that a ListBox with a horizontal layout in a WrapPanel suits my scenario perfectly.
I posted the solution in another question.

Related

devexpress wpf gridcontrol disable context menu

I have a DevExpress grid control and I want to disable the default context menu that appears when I right click the Grid column headers. To disable this functionality I handled the PreviewMouseRightButtonDown and PreviewMouseRightButtonUp
private void UserControl_PreviewMouseRightButtonDown_Up(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}
This is not an acceptable solution. There should be should be something on grid control.
Please set the TableView.IsColumnMenuEnabled property to control whether the column context menu is shown when an end-user right-clicks a column's header.
You can read more about all avilable DXGrid's context menus and its customization here: Context Menus
Set IsColumnMenuEnabled="False" on your TableView.
If you want disable specific context menu item you can manage it by binding
<dxb:BarButtonItem Name="contexMenuTransmitPendingClaim"
Command="{Binding Path=(dxb:GridPopupMenuBase.GridMenuInfo).View.DataContext.TransmitPendingClaimCommand,
RelativeSource={RelativeSource Self}}"
Content="Transmit Pending Claim"
IsEnabled="{Binding Path=(dxb:GridPopupMenuBase.GridMenuInfo).View.DataContext.SelectedCusHisViewRefillHistory.IsPendingClaimsActive,
RelativeSource={RelativeSource Self},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>

WPF bind a control visibility to the focused property of another control

I have a combobox that displays a list of items, and I want to place a button next to it which triggers a command to see the details of the selected item. So far, so good. Now I want the button to be visible only if the combobox has focus (or is in "edit" mode, but not only when the popup is open).
I thought I could bind the visibility of the button to some focus property of the combobox, something like this:
<Button Content="Details" Visibility="{Binding ElementName=elementListComboBox,
Path=IsFocused, Converter={StaticResource Bool2VisibilityConverter}}"/>
But I found no way to know if the control I want is focused or not. I looked at the FocusManager.FocusedElement, but I don't know how to get the focused control I want inside the binding. Is there a way to achieve this in XAML?
Ok, the way to get this working as I wanted is this:
<Button Command="{Binding SomeCommand}"
Content="Details"
Focusable="False"
Visibility="{Binding ElementName=elementListComboBox,
Path=IsKeyboardFocusWithin,
Converter={StaticResource Bool2VisibilityConverter}}"/>
Two key factors here: bind the button's visibility to IsKeyboardFocusWithin property of the combobox, and set the button's Focusable property to false, else it will get collapsed when you want to click on it.
Hope this is useful.

Disable button when datagrid is in edit mode

I have usercontrol with a DataGrid binded to an observablecollection of items and two buttons: save changes and discard changes.
My problem is that when the user is editing a datagridrow the buttons remain clickable but aren't executed.
Is there a way to disable the buttons when the DataGrid is in edit mode?
I tried this code with no success:
<Button Content="SaveChanges" Command="{Binding Path=CmdSaveChanges}"
IsEnabled="{Binding ElementName=MyDataGrid, Path=IsEditing, Converter={StaticResource InverseBooleanConverter}}" />
You are binding to an IsEditing property of an element named MyDataGrid which presumably is a DataGrid. However, DataGrid has no such property.
Although DataGridCell has an IsEditing property, there is no easy way to get the currently editing cell. DataGrid.CurrentCell will not give you the DataGridCell, but only a DataGridCellInfo.
You are perhaps better of with attaching handlers to the DataGrid's BeginningEdit and CellEditEnding events.

Changing a property of something in XAML from a button click

Given the following bit of XAML
<Border Name="Brder" Visibility="Visible" Width="10" Height="10" Background="Red"></Border>
<Button Content="Hide"></Button>
How can I set the button click to change the visibility of the border (not using code behind)?
I know it's trivial in code behind and the routed click event only seems to allow storyboard manipulation?
Thanks
If you change the Button to a ToggleButton you can bind visibility to IsChecked of the ToggleButton using XAML only. Also remember that Expanders support this behavior as well and you can laways style them if you don't like the default look.

WPF - prevent ListBox item selection

I would like to prevent selection of ListBoxItems in my ListBox. My DataTemplate has a checkbox and this should be the only thing the user can click or select.
How can I do that?
Thanks!
This is almost a duplicate question. In fact, you're asking two questions here:
Either style your ListBoxItem so that it doesn't show selection (look elsewhere on SO for that answer), or replace ListBox with ItemsControl if you don't need the other features that ListBox provides.
Bind your checkbox's IsChecked property to the parent ListBoxItem.IsSelected property:
<CheckBox
IsChecked="{Binding
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=ListBoxItem},
Path=IsSelected}"
/>
When your user will try to (un)check your checkboxes then item become 'active' in some way. And focused style will be applied. As far as i know there is no way to disable selection(because if you did your checkboxes will not work) but you can override focused(or selected) style of your listbox items

Resources